0

After getting a new Oauth2 access token using the Google API for PHP, the newly given access token reports expired, with no refresh token available. Below, I send a CURL call through PHP to get my new access token, which works fine:

    //Get new access token from Google
    $curl = curl_init();
    curl_setopt($curl,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($curl,CURLOPT_FOLLOWLOCATION,true);
    curl_setopt($curl,CURLOPT_POST,1);
    curl_setopt($curl,CURLOPT_POSTFIELDS,array(
        'refresh_token' => $row['refresh_token'],
        'client_id' => $clientId,
        'client_secret' => $clientSecret,
        'grant_type' => 'refresh_token',));
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);
    $json_data = curl_exec($curl);

This successfully returns a JSON representation of the new access token. However, when I attempt to use that JSON to add some basic text to a user's Glass timeline, I am given the following error:

An error occurred: The OAuth 2.0 access token has expired, and a refresh token is not available. Refresh tokens are not returned for responses that were auto-approved.

The code I am using to attempt to attempt the insertion is shown below:

    //Configure the Google Client
    $client = new Google_Client();
    $client -> setClientId($clientId);
    $client -> setClientSecret($clientSecret);
    $client -> setRedirectUri($redirectUri);
    $client -> setAccessToken($json_data);
    $client -> setScopes(array(
        'https://www.googleapis.com/auth/glass.timeline',
        'https://www.googleapis.com/auth/userinfo.profile',
        'https://www.googleapis.com/auth/glass.location',
        'https://www.googleapis.com/auth/userinfo.email',
        'https://www.googleapis.com/auth/plus.me'));

    //Start a Mirror service
    $mirror_svc = new Google_Service_Mirror($client);

    //Filter the text to prevent ultimate destruction
    $text = filter_var($_POST['t_text'],FILTER_SANITIZE_SPECIAL_CHARS);

    //Attempt to add text to timeline
    $result = (insertTimelineItem($mirror_svc,$text,null,null,null) ? 'success' : 'failure');

I feel like I'm missing something simple, and I'm just not sure what is is. Any insight would be greatly appreciated.

Note: the post variable is only temporary and used for testing.

Legendary_Linux
  • 1,509
  • 1
  • 11
  • 17

1 Answers1

0

As it turns out, I just needed to add

    if($client -> isAccessTokenExpired()){
            $client -> refreshToken($row['refresh_token']);
    }
Legendary_Linux
  • 1,509
  • 1
  • 11
  • 17