Google API Client Library for JavaScript

repository·master·Indexed 25 days ago

https://github.com/google/google-api-javascript-client

A client-side JavaScript library providing flexible access to various Google APIs. It features a Promises/A+ conformant interface for API requests, support for batching multiple requests into a single HTTP round-trip, and dynamic API loading via Discovery Documents. The library is accessed via the `gapi` object, with initialization handled by `gapi.client.init()`.

Tokens
6K
Snippets
13
Records
42
Agent score
86%

What's inside google-api-javascript-client

  1. Overview of Google API Client Library for JavaScript

    master

    The Google API Client Library for JavaScript provides developers with simple and flexible access to a wide range of Google APIs within JavaScript client applications.

    Note: This repository contains the client library configuration and metadata, but it does not contain the actual source code for the gapi client itself.

  2. Install and use Compass for CSS/SCSS

    master

    The template uses SCSS for maintainability. You can either edit the generated files in /theme/css directly or use Compass to compile SCSS files from /theme/scss.

    To install Compass via RubyGems:

    sudo gem update --system
    sudo gem install compass

    To watch for changes and automatically recompile SCSS files into CSS:

    $ cd io-2012-slides
    $ compass watch

    To watch for changes and output unminified (expanded) CSS files:

    $ compass watch -s expanded
    $ cd io-2012-slides
    $ compass watch
  3. Find an API's Discovery Document URL

    master

    To use the JavaScript client library with a specific Google API, you must load its Discovery Document.

    1. Check API Documentation: If the API documentation provides an explicit discovery URL, use it directly.
    2. Construct Default URL: If no URL is provided, construct it using the following pattern:

    https://www.googleapis.com/discovery/v1/apis/{api_name}/{api_version}/rest

    Example for Translate API v2: https://www.googleapis.com/discovery/v1/apis/translate/v2/rest

  4. Initialize the Google API Client Library

    master

    To use the library, you must first load the JavaScript client library via gapi.load('client', callback) and then initialize it using gapi.client.init().

    Initialization requires an apiKey. If you need to access private user data, you must also provide a clientId and scope. To use specific Google APIs (like People or Drive) with the discovery-based method, you must provide their corresponding Discovery Document URLs in the discoveryDocs array.

    // 1. Load the JavaScript client library.
    gapi.load('client', function() {
      // 2. Initialize the JavaScript client library.
      gapi.client.init({
        'apiKey': 'YOUR_API_KEY',
        'discoveryDocs': ['https://people.googleapis.com/$discovery/rest'],
        'clientId': 'YOUR_WEB_CLIENT_ID.apps.googleusercontent.com',
        'scope': 'profile',
      }).then(function() {
        // 3. Make the API request.
        return gapi.client.people.people.get({
          'resourceName': 'people/me',
          'requestMask.includeField': 'person.names'
        });
      }).then(function(response) {
        console.log(response.result);
      }, function(reason) {
        console.log('Error: ' + reason.result.error.message);
      });
    });
  5. Access Google API Client Library documentation

    master

    The library documentation is organized into several key areas to help you integrate Google services:

    • Getting Started: Initial setup and first steps.
    • Auth: Guidance on authentication and authorization flows.
    • Batch: Information on performing multiple API requests in a single batch.
    • Discovery Documents: How to use discovery documents to interact with APIs.
    • FAQ: Frequently asked questions.
    • Promises: Documentation on using Promises with the library.
    • Reference: Detailed API reference.
    • Samples: Practical code examples and samples.
  6. Perform Batch requests with Promises

    master

    When using gapi.client.newBatch(), do not invoke the .then() method on individual requests until after they have been added to the batch using batch.add(request). If .then() is called before adding to the batch, the request will be sent immediately as a single request instead of part of the batch.

    var req1 = ... // Instantiate
    var req2 = ... // Instantiate
    var batch = gapi.client.newBatch();
    batch.add(req1);
    batch.add(req2);
    req1.then(...);
    batch.then(...);
    req2.then(...);
  7. Migrate from callbacks to promises

    master

    To migrate from the legacy .execute(callback) pattern to the promise interface, note that the result parameter of the fulfilled promise is equivalent to the first parameter in the callback.

    Callback Pattern (Legacy):

    gapi.client.request({
      'path': 'plus/v1/people',
      'params': {'query': name}
     }).execute(function(resp, rawResp) {
       processResponse(resp);
     });

    Promise Pattern (Recommended):

    gapi.client.request({
      'path': 'plus/v1/people',
      'params': {'query': name}
     }).then(function(resp) {
       processResponse(resp.result);
     });
    gapi.client.request({
      'path': 'plus/v1/people',
      'params': {'query': name}
     }).then(function(resp) {
       processResponse(resp.result);
     });
  8. Handle individual request promises within a batch

    master
    Even when added to a batch, each individual request can be treated as a standalone promise. If you invoke .then() on an individual request object, the promise will fulfill or reject with the specific value for that request, behaving exactly as if the request had been executed individually.