刷新后,Oauth2访问令牌报告已过期(Google Glass相关)

时间:2014-03-06 17:40:33

标签: php oauth oauth-2.0 google-glass

使用Google API for PHP获取新的Oauth2访问令牌后,新授予的访问令牌报告已过期,没有可用的刷新令牌。下面,我通过PHP发送一个CURL调用来获取我的新访问令牌,这可以正常工作:

    //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);

这成功返回新访问令牌的JSON表示。但是,当我尝试使用该JSON将一些基本文本添加到用户的Glass时间轴时,我收到以下错误:

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.

我尝试尝试插入的代码如下所示:

    //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');

我觉得我错过了一些简单的东西,我只是不确定是什么。任何见解将不胜感激。

注意:post变量只是临时变量并用于测试。

1 个答案:

答案 0 :(得分:0)

事实证明,我只需要添加

    if($client -> isAccessTokenExpired()){
            $client -> refreshToken($row['refresh_token']);
    }