How to get refresh_token from Google API ?

592 views
Skip to first unread message

Harsha M V

unread,
Jun 23, 2012, 8:52:32 AM6/23/12
to google-api...@googlegroups.com
i am trying to access Google Analytics Account and its Data while the User is Offline. For some reason am not able to get the refresh_token.

This is what i am doing.

    //If No token is set generate Auth URL
    if (!$client->getAccessToken()) {
        $authUrl = $client->createAuthUrl();
        header('Location: $authUrl'));
    }


This sends me to Google and i Grant access to the Account

    //Settings
    $client->setClientId('70181844.apps.googleusercontent.com');
    $client->setClientSecret('Co81SxK65fxAFHovtG0ji');
    $client->setRedirectUri('http://localhost/gitgrow1/index.php');
    $client->setDeveloperKey('AIzaTSPKHNh7pMSCJ95SrGkxm2E92eV4r75Y');
    $client->setScopes(array('https://www.googleapis.com/auth/userinfo.profile',
        'https://www.googleapis.com/auth/userinfo.email',
        'https://www.googleapis.com/auth/analytics.readonly'));
    $client->setAccessType('offline');
    $client->setApprovalPrompt('auto');


Now if Google returns a Code i authenticate and try to get the Token.

    if (isset($_GET['code'])) {
        $client->authenticate();
        $_SESSION['token'] = $client->getAccessToken();
        echo "<pre>";
        print_r($_GET);
        print_r(json_decode($client->getAccessToken()));
        if (!$client->getAccessToken()) {
            $authUrl1 = $client->revokeToken();
            print_r($authUrl1);
        }
        echo "</pre>";
    }

This only Gives me the following

    Array
    (
        [code] => kACAH-1Ng2_8mPWAXS1-rUk8Z2q8ALn_FCErtp2lrYyvrSBCpo2J9Ahjpo7MjO6LNDrK9cxePMc5eGwUGGZ-B7Qshh1eSmEzwizNtpB9PTVJZ-GdTKQxXR-n18
    )
    stdClass Object
    (
        [access_token] => ya29.AHES6ZTisW_qS-yG9EU9GClF8utib0BnA81oT_voECjwvCZ4HleRCQ
        [token_type] => Bearer
        [expires_in] => 3600
        [id_token] => eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiYXVkIjoiNzAxODE4MjcxNjQ0LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiY2lkIjoiNzAxODE4MjcxNjQ0LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiaWQiOiIxMTAwODQzMzA1OTM4MDAzOTY3NjQiLCJlbWFpbCI6ImhhcnNoYUBtaW5rNy5jb20iLCJ2ZXJpZmllZF9lbWFpbCI6InRydWUiLCJ0b2tlbl9oYXNoIjoiaExkN3RnSnJrZGpITExObWZrZktvZyIsImhkIjoibWluazcuY29tIiwiaWF0IjoxMzQwNDU0ODc2LCJleHAiOjEzNDA0NTg3NzZ9.Q691XUJBwC6EEKUv3wy94Ab76gL6wZz3lcwSHlixLyjMgGBPWRzwMQcTMyrWoh-V9nxhp3nEaJZ1KfzI9j4ufUdpdX4tWiz8shdhoNJJHbSYKeEer3foKyjv_zped55BLbVA2IcJT9Tnw84f1toOqrVzDDqLsrWLQbtPFY49dak
        [created] => 1340455163
    )




I am expecting a refresh_token here instead of id_token.

Can someone guide me what i am doing wrong ? I am using the official PHP Client for google API

Chirag Shah

unread,
Jun 23, 2012, 10:49:52 AM6/23/12
to google-api...@googlegroups.com
You're not getting a refresh token because you're using $client->setApprovalPrompt('auto');
To be specific, you'll only receiving a refresh token upon the first exchange of the authorization code, at which point it should be saved in a database.

For more information about approval_prompt=auto, please see:

"When your application receives a refresh token, it is important to store that refresh token for future use. If your application loses the refresh token, it will have to re-prompt the user for consent before obtaining another refresh token. If you need to re-prompt the user for consent, include the approval_prompt parameter in the authorization code request, and set the value to force."
Reply all
Reply to author
Forward
0 new messages