Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

authenticating a service account to call a Google API with JavaScript client library

I want to make JSON-RPC calls from localhost (WAMP environment) to the Google FusionTables API (and a couple of other APIs) using the Google Client Library for JavaScript

Steps I have taken:

  1. setup a project on the Google Developer Console
  2. enabled the FusionTables API
  3. created a service account and downloaded the JSON file.
  4. successfully loaded the JS client library with the auth package: gapi.load('client:auth2', initAuth);
  5. constructed the init method parameter the following 3 ways:
    • the downloaded JSON verbatim
    • the downloaded JSON modified to include the scope
    • just the client ID and scope
  6. tried (and failed) to initialize the GoogleAuth instance: gapi.auth2.init(params)

    function failed(reason) {
        console.log(reason);
    }
    gapi.load('client:auth2', initAuth);

    function initAuth() {

        var APIkey = 'MY API KEY';
        gapi.client.setApiKey(APIkey); //I understand this to be unnecessary with authorized requests, included just for good measure

        var GDTSAKey = 'MY SERVICE ACCOUNT KEY';
        var scopes = 'https://www.googleapis.com/auth/fusiontables';
        gapi.auth2.init({
            client_id: "101397488004556049686",
            scope: 'https://www.googleapis.com/auth/fusiontables'
        }).then(signin, failed("couldn't initiate"));
        //passing the downlaoded JSON object verbatim as parameter to init didn't work either
    } //initAuth()

    function signin() {
        gapi.auth2.getAuthInstance().signIn().then(makeAPIcall), failed("couldn't sign-in");
    }

    function makeAPIcall(){
        gapi.client.load('fusiontables', 'v2', function(){
            var tableId = '1PSI_...';
            var table = gapi.client.fusiontables.table.get(tableId);
            document.querySelector("#result").innerHTML = table;            
        });
    }

based on JS client library >> Samples

the gapi.auth2.init method invokes the second callback (which I understand to be an error handler): failed("couldn't initiate"), but then, curiously, I also get `couldn't sign in' which could only have originated from within the provided success handler. What's going on? How do I get this to work?

Note: I am only willing to try the CORS/xhr, if there is no way to do it with JS client lib.

like image 728
Ehsan Amini Avatar asked Oct 03 '16 06:10

Ehsan Amini


People also ask

How do I authenticate my Google services account?

To use Google Service Account authentication, you will need a service account key for your service account. This file should be download in JSON format. To learn about creating and managing service account keys, see the Google IAM documentation on creating and managing service account keys.

How do I authenticate my Google API?

User accounts With a user account, you can authenticate to Google APIs and services in the following ways: Use the gcloud CLI to set up Application Default Credentials (ADC). Use the gcloud CLI to generate access tokens. Use your user credentials to impersonate a service account.

How do I use Gmail API with service account?

Enable the GMail APIFrom the project, click on ENABLE APIS AND SERVICES and search for the Gmail API. Click on ENABLE. You now have the Gmail API enabled for your project. All you need now is to create some credentials for using the API.


1 Answers

What's going on?

You are trying to use a service account with the Google JavaScript client library which does not support service accounts.

How do I get this to work?

Switch to Oauth2 authentication or if you must use a service account switch to a server sided language like PHP or python for example. Which support service account authentication.

like image 78
DaImTo Avatar answered Sep 30 '22 19:09

DaImTo