Google-api-javascript-client: gapi.client.init() with access token?

Created on 11 Jun 2017  路  11Comments  路  Source: google/google-api-javascript-client

I'm using Google Firebase to authenticate users, and after the user is authenticated I get an access token.

I know that I can use this access token in order to work with google api's, but I can't find any way to work with gapi using this access token.

Is there any way to use gapi assuming I already have an access token?

What I'm trying to do is access the user calendar after simply authenticating with Firebase.

Most helpful comment

@theunreal I would recommend using gapi.client.setToken({access_token:'.....'}), we added this method recently but haven't documented it yet.

All 11 comments

Have you tried gapi.auth.setToken({access_token:'.....'}) ? It's possible there is a newer/better way to do this, though.

@theunreal I would recommend using gapi.client.setToken({access_token:'.....'}), we added this method recently but haven't documented it yet.

Thanks! @bsittler @TMSCH

@TMSCH Can you explain how to use this method ??

I am having trouble in setting up a solution for a client and I'm wondering whether what I'm attempting to do is even possilbe.
My client is a teacher. They have a youtube channel and they would like their students to be able to post coursework videos to the channel directly.
I need to keep track of which video was submitted by who and when so I'm trying to use google APIs in the client web browser to submit videos.
My problem is that I can't find a way of automatically authenticating the user - the closest I get pops up a dialogue box for the user to log themselves in, which is no good for me.
I have tried generating a token on the server and passing the token to the client to use but that doesn't work.

Any pointers or suggestions would be very much appreciated

@TMSCH
Can you explain a bit more on how to use this?
My problem is: If I use firebase for auth, then try to send the token to GAPI like so:

function initClient() {
  window.gapi.client.init(googleCalendarConfig)
  .then(_ => {
    console.log('init ok')
  })
  .catch(error => {
    dispatch('authenticationError', {error, note: 'Error on GAPI init.'})
  })
}
window.gapi.load('client', initClient)
window.gapi.client.setToken({access_token:'.....'})

Then I still get this error:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}

Apparently gapi.auth2.getInstance().isSignedIn.get() is still false, so doing gapi.client.setToken({access_token:'.....'}) is clearly not enough to be able to work with GAPI. Do you know how to activate and authenticate the gapi instance with an access token from One-Tap (or Firebase for that matter)?

@adrienjamot if you can obtain an Access Token from your backend (for instance using a Refresh Token) no need to "authenticate" the user. Just pass the access token to gapi.client.setToken({access_token: 'ACCESS_TOKEN_RETURNED_FROM_BACKEND'}).

@mesqueeb make sure you pass the access_token to the setToken call, not the id_token (I saw this in another of the issue you opened). ID Token is meant for authenticating the user in your backend, Access Token is meant for authorizing requests to Google APIs.

@TMSCH first of all, thanks for your help!

I'm having the exact same problem. Passing the access token I get back from the server to gapi.client.setToken({ access_token: 'ACCESS_TOKEN_RETURNED_FROM_BACKEND' }) is simply not enough as gapi.auth2.getInstance() just returns null.
I wonder what else is missing as this seems to be the recommended approach (?)

@luis-almeida see #401 for more details on why this is not possible, and how to make it work.

@TMSCH @mesqueeb I am unable find any documentation to get new access token in exchange of expired access token using gapi.client. As per my requirement I have to avoid login popup once user gives their permission. Also I would be able to pass these permission to any other devices.

I see a solution to get new access token by exchanging refresh token using PHP client. https://github.com/googleapis/google-api-php-client#obtainingaccesstokens

Do we need to go through Security Assessment if you combine both PHP and JS client?
Or
Do we have any other ways to solve this using JS client?

I would appreciate every helping advice!

@theunreal I would recommend using gapi.client.setToken({access_token:'.....'}), we added this method recently but haven't documented it yet.

Is this documented somewhere now? This answer is 2 years old and I feel it's still very hard to make GAPI client work predictably.

If you get an access token from Chrome.identity service and assign it to gapi client, it sometimes doesn't work.

Hello everybody,
I'am facing issue with google calendar. The issue is "when user1 is connecting his google calendar with our website account and then logout his account from our website only and then user2 connect his google calendar with his account of our website and then logout. So what happening is, now if user1 is login to his account then he is not able to access his google calendar and the error image link is shown below".
(https://user-images.githubusercontent.com/38031527/88327203-730e2b00-cd44-11ea-86d2-23bea41cf789.jpeg)
.

Please help me out, what should I need to do to make this process work. I'm stuck with this issue from long time. I must be missing something.

Was this page helpful?
0 / 5 - 0 ratings