google / google-api-javascript-client

Google APIs Client Library for browser JavaScript, aka gapi.
Apache License 2.0
3.19k stars 1.05k forks source link

Cann't get refresh_token with GoogleAuth.signIn #538

Open nmquaan-bigbom opened 5 years ago

nmquaan-bigbom commented 5 years ago

Hi there, Please help resolve my problem, I want to get refresh_token when user login with google use javascript. This is my code.

<script>
        window.gapi;
        window.ggInit = options => {
            gapi.load('auth2', function () {
                window.gapi = gapi;
                gapi.auth2.init(options).then(() => {
                    window.GoogleAuth = gapi.auth2.getAuthInstance();
                });
            });
        };
</script>
if (window.GoogleAuth) {
            window.GoogleAuth.signIn({
                scope: 'profile email https://www.googleapis.com/auth/adwords',
                access_type: 'offline',
                prompt: 'consent',
            })
                .then(googleUser => {
                    this.loginGoogleCallback(googleUser);
                })
                .catch(err => {
                    console.log('Err', err);
                });
}
This is response data
access_token: ""
expires_at: 1561026652957
expires_in: 3600
first_issued_at: 1561023052957
id_token: ""
idpId: "google"
login_hint: ""
scope: "email profile https://www.googleapis.com/auth/userinfo.profile openid https://www.googleapis.com/auth/adwords https://www.googleapis.com/auth/userinfo.email"
session_state: {extraQueryParams: {…}}
token_type: "Bearer"
theAbeGonzalez commented 5 years ago

If I'm not wrong the refresh token is only provided during the first authentication. You can revoke permission to your app and try to sign in again in order to retrieve it.

nmquaan-bigbom commented 5 years ago

Hi @abrahamgnz, Thanks for the feedback. I try to do it and re-check my problem.

nmquaan-bigbom commented 5 years ago

Hi @abrahamgnz , I did it again according to your instructions, but still not true to the results I expected. Do you have any solution to manually generate refresh_token. Thanks!

HerrPeter commented 5 years ago

If you haven't solved your issue yet, you can get an authorization code from gapi.auth2.getAuthInstance().currentUser.get().grantOfflineAccess(). Then using the auth code, you can send it to your backend and exchange it for a refresh_token from google.

A problem you should encounter: by calling this grantOfflineAccess method, the user will be prompted to select their account, THEN they'll be prompted to consent to the scopes. This happens no matter you've called GoogleAuth.signIn() or not. I believe this to be an issue. If you encounter this issue, let the devs hear your frustration.

Best, Peter

gpashis commented 4 years ago

@HerrPeter Is there any way to avoid the prompt to select user account and get new access token after it expires?

bionicles commented 4 years ago

google sign in refresh docs are unclear and this whole issue is an annoying mess of callbacks, tokens, conditionals (oh, it works once but not again!) and forces devs to waste our time and forces users to click way too many times

argarner commented 4 years ago

Please see this solution for refreshing tokens client side => https://github.com/google/google-api-javascript-client/issues/643#issuecomment-642350903