【问题标题】:Oauth2 Access Token reports expired after Refresh (Google Glass Related)Oauth2 访问令牌报告在刷新后过期(Google Glass 相关)
【发布时间】:2014-03-07 04:05:29
【问题描述】:

使用 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 变量只是临时变量,用于测试。

【问题讨论】:

    标签: php oauth oauth-2.0 google-glass


    【解决方案1】:

    事实证明,我只需要添加

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

    【讨论】:

    • 是的......在谷歌眼镜示例代码中,他们提到他们没有处理令牌刷新
    猜你喜欢
    • 1970-01-01
    • 2021-12-19
    • 2018-04-07
    • 2017-04-14
    • 2021-05-09
    • 2018-06-05
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    相关资源
    最近更新 更多