5

I'm trying to implement resumable uploads for youtube using the javascript client API. I've managed to get the login working using the g-signin2 button, but I can't get the access_token to send with the requests. This is how I render the sign in button:

<span
  class="g-signin2"
  data-onsuccess="youtubeUploaderSigninCallback"
  data-scope="https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/youtube">
</span>

And I'm using the following script files loaded from google (I can't seem to get one working without the other)

<script src="https://apis.google.com/js/platform.js" async defer></script>
<script src="https://apis.google.com/js/client.js" async defer></script>

I'm using code edited samples from https://developers.google.com/youtube/v3/code_samples/javascript#upload_video for resumable uploads using XHR. But these examples use the auth functions marked as deprecated here: https://developers.google.com/api-client-library/javascript/reference/referencedocs So the main thing I changed is using the auth2 and g-signin2 instead of result.access_token and g-signin.

So I've been trying to use the auth2 functions to get the access tokens, but the access_token property is undefined when I execute the following code

gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse()

The way I'm trying to get the access token is documented here: https://developers.google.com/api-client-library/javascript/features/cors#making-authenticated-requests

So these are my options, I guess:

  1. find a way to get the access token out of the user object
    • (I found that user.hg.access_token contains the token I need
    • but "hg" seems a minified property to me
    • so I'm not intending to use this as it might change with updates
    • EDIT: as pointed out in the comments hg is a minified property indeed and now no longer exists
  2. add progress events to gapi.client.request()
    • but the xhr object doesn't seem to be exposed
  3. use code documented as deprecated

Can anyone help me with this? I've been looking for an acceptable solution for a few hours and I feel like crying now... all alone and desperate.

Thanks in advance, Robbe

PS: I've also posted this question on google groups

4
  • FYI: I am now using the "g-signin" button (not g-signin2) and everything works, but I don't know if this is very future proof. So answers are still welcome. Commented Aug 4, 2016 at 9:32
  • I wish you the best of luck in finding an answer. According to developers.google.com/identity/sign-in/web/…, the AuthResponse is supposed to have an access token. Strange that it's not there. Commented Sep 13, 2016 at 17:05
  • I implemented the same thing with g-signin2, and I did use hg, but it proved you right: the property is gone now and and is renamed to Zi. Still looking for a way to properly fetch it from the response. Commented Sep 20, 2016 at 4:56
  • For now I'm just sticking with the g-signin option. This is still working and I hope it stays that way until g-signin2 is "fixed". Glad I did not depend on the hg property. Thanks for sharing, I'll put this in the question. Commented Sep 20, 2016 at 17:05

2 Answers 2

13
gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().access_token
Sign up to request clarification or add additional context in comments.

3 Comments

To improve your answer, you should add some explanation of what your answer does, how to use it and how it answers the question.
stackoverflow.com/posts/50864432 getAuthResponse(true) without true the access token is undefined
Thanks for the above comment! most documentation doesn't include the true parameter. That solved my problem
0

This snippet is working perfectly

gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().access_token

gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().id_token

etc...

1 Comment

Isn't this the same answer provided in 2018?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.