GoogleUser.getAuthResponse() doesn't contain access_token

前端 未结 2 1028
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 13:35

UPDATE2: I revisited this issue and have solved the problem by carefully following the doco linked below. But first, for those who are struggling with this,

相关标签:
2条回答
  • 2021-01-01 14:20

    I know this question is fairly old, but it appears first when googling for ".getAuthResponse() doesn't have access_token," which is how I got here.

    So for those of you in 2016 (and maybe later) here's what I have found out

    There's a secret argument on .getAuthResponse, not documented anywhere I have found. If you would run the following in your app

    console.log(gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse);
    

    You would see that you get the following (copy/pasted from my console)

    function (a){if(a)return this.hg;a=.HE;var c=.rf(this.hg);!a.Ph||a.dL||a.Lg||(delete c.access_token,delete c.scope);return c}

    This shows that the .getAuthResponse() function looks for an argument, and as far as I can tell doesn't even check its value -- it simply checks if it is there and then returns the whole object. Without that function, the rest of the code runs and we can see very clearly it is deleting two keys: access_token and scope.

    Now, if we call this function with and without the argument, we can check the difference in the output. (note: I used JSON.stringify because trying to copy/paste the object from my browser console was causing me some issues).

    console.log(JSON.stringify(gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse()));
    console.log(JSON.stringify(gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse(true)));
    

    getAuthResponse() object

    { "token_type":"Bearer", "login_hint":"<Huge mess of letters>", "expires_in":2112, "id_token":"<insert your ridiculously long string here>",...}

    getAuthResponse(true) object

    { "token_type":"Bearer", "access_token":"<an actual access token goes here>", "scope":"<whatever scopes you have authorized>", "login_hint":"<another mess of letters>", "expires_in":2112, "id_token":"<Insert your ridiculously long string here>", ...}

    Hope this helps you guys!

    0 讨论(0)
  • 2021-01-01 14:31

    Figured out the fix for this. Turns out that if we don't provide the login scope config in gapi.auth2.init it doesn't return access_token in getAuthResponse. Please call gapi.auth2.init as given below and access_token will be present.

    gapi.auth2.init({
      client_id: <googleClientID>,
      'scope': 'https://www.googleapis.com/auth/plus.login'
    })

    0 讨论(0)
提交回复
热议问题