Google-api-javascript-client: gapi.client.init call never returns

Created on 16 Dec 2016  路  4Comments  路  Source: google/google-api-javascript-client

I am trying to use the auth client in chrome extension.
I added content_security_policy in manifest.json
"content_security_policy": "script-src 'self' https://apis.google.com/; object-src 'self'"

Now it shows no errors in console.
console.log('1'); is executed.
But console.log('2'); in the example below is never executed.
Can you please help ?

Please note my source code below

And js/google_login.js contents below:

var apiKey = 'AIzaSyCJX8auossfAykk1-UxTsJuDDq79plNAOA';

// Enter the API Discovery Docs that describes the APIs you want to
// access. In this example, we are accessing the People API, so we load
// Discovery Doc found here: https://developers.google.com/people/api/rest/
var discoveryDocs = ["https://people.googleapis.com/$discovery/rest?version=v1"];

// Enter a client ID for a web application from the Google API Console:
// https://console.developers.google.com/apis/credentials?project=_
// In your API Console project, add a JavaScript origin that corresponds
// to the domain where you will be running the script.
var clientId = '302294786959-tjqfcbdlr1cf8s23sdfuuonhmvcqe36s.apps.googleusercontent.com';

// Enter one or more authorization scopes. Refer to the documentation for
// the API or https://developers.google.com/people/v1/how-tos/authorizing
// for details.
var scopes = 'email profile';

function initClient() {
console.log('1');
gapi.client.init({
apiKey: apiKey,
discoveryDocs: discoveryDocs,
clientId: clientId,
scope: scopes
}).then(function(opt_onFulfilled, opt_onRejected) {
console.log('2');
// Handle the initial sign-in state.
var isSignedIn = gapi.auth2.getAuthInstance().isSignedIn.get();

    handleAuthClick();

});

}

function handleAuthClick(event) {
console.log('3');
gapi.auth2.getAuthInstance().signIn();
}

// Load the API and make an API call. Display the results on the screen.
function makeApiCall() {
gapi.client.people.people.get({
resourceName: 'people/me'
}).then(function(resp) {

    console.log(resp.result.names[0].givenName);
});

}
gapi.load('client:auth2', initClient);

auth

Most helpful comment

Chrome Extension is not a supported environment. See https://developers.google.com/api-client-library/javascript/start/start-js#supported-environments - Chrome Extensions run in a restricted execution context.

All 4 comments

@Nilesh-P could you try again? We recently made some changes that may catch the error raised.

Update this bit of code before to display the error if any is raised:

```
gapi.client.init({
apiKey: apiKey,
discoveryDocs: discoveryDocs,
clientId: clientId,
scope: scopes
}).then(function() {
console.log('2');
}, function(error) {
console.log(error);
});

Chrome Extension is not a supported environment. See https://developers.google.com/api-client-library/javascript/start/start-js#supported-environments - Chrome Extensions run in a restricted execution context.

For chrome extensions we should use Chrome Identity API

@jsuryahyd yes but gapi has all the function already written. i.e) Google Drive API file export feature.

Was this page helpful?
0 / 5 - 0 ratings