【问题标题】:Can't get an offline access token with the Google PHP sdk 1.0.0无法使用 Google PHP sdk 1.0.0 获取离线访问令牌
【发布时间】:2014-04-11 15:58:11
【问题描述】:

我无法使用此代码获取离线访问令牌... 为什么?

$client = new Google_Client();
$client->setApplicationName('MyAppName');
$client->setScopes(array('https://www.googleapis.com/auth/plus.me'));
$client->setClientId('MyCientID');
$client->setClientSecret('MyClientSecret');
$client->setRedirectUri('http://mydomain.com/googlecallback');
$client->setApprovalPrompt('force');
$client->setAccessType('offline');
$client->setDeveloperKey('MyDeveloperKey');
$plus = new Google_Service_Plus($client);
header('Location: '.$client->createAuthUrl());

这是重定向到谷歌登录页面,它只要求一个 1 小时的访问令牌... 我迷失在黑暗中......

非常感谢!

编辑:

这是我的登录页面代码:

$client = new Google_Client();
$client->setClientId('qvdsfvqdsf');
$client->setClientSecret('qsdfvqsdf');
$client->setRedirectUri('?a=callback');
$client->setDeveloperKey('qdcQSDCQSD');
$client->setApprovalPrompt('auto');
$client->setAccessType('offline');
$client->setScopes(array('https://www.googleapis.com/auth/plus.me'));
$plus = new Google_Service_Plus($client);

if($_GET['a'] == 'authorize'){
    header('Location: '.$client->createAuthUrl());
}
elseif($_GET['a'] == 'callback' && isset($_GET['code']) && !isset($_GET['error'])){
    $client->authenticate($_GET['code']);
    if($client->getAccessToken()){
        STORE ACCESS TOKEN
    }
}

还有我的 API 使用情况:

$client = new Google_Client();
$client->setClientId('qsdfsqd');
$client->setClientSecret('qsdfqsd');
$client->setRedirectUri('qdfq');
$client->setDeveloperKey('sdfvsdf');
$client->setApprovalPrompt('auto');
$client->setAccessType('offline');
$client->setScopes(array('https://www.googleapis.com/auth/plus.me'));
$plus = new Google_Service_Plus($client);
$client->setAccessToken('STORED ACCESS TOKEN');
$activities = $plus->activities->listActivities('me', 'public', array('maxResults'=>10));

我做错了什么?

【问题讨论】:

    标签: oauth-2.0 google-plus google-oauth google-api-php-client


    【解决方案1】:

    如果用户之前已通过 ClientID 进行身份验证而没有请求离线模式,然后您请求离线模式,它不会给您刷新令牌。

    用户需要从应用中取消对自己的授权,然后在应用请求离线模式时重新授权。

    【讨论】:

      【解决方案2】:

      终于找到答案了:

      使用 $client->setApprovalPrompt('force'); 阻止了正常的 Google 令牌刷新过程,而 $client->setApprovalPrompt('auto'); 这样做就像一个魅力。

      谢谢。

      【讨论】:

      • 很高兴您工作正常,但这不是答案。还有一些其他问题得到了解决,这个代码更改只是巧合。
      【解决方案3】:

      您需要将“http://mydomain.com/googlecallback”URL 设置为 application settings page 上的重定向 URI。

      $client->createAuthUrl() 方法创建身份验证页面的 URL。转到该页面并授权应用程序后,Google 会将您重定向回/googlecallback,并带有一个名为“code”的查询字符串参数,您应该将其传递给客户端的 authenticate() 方法。只有这样您才能访问令牌。

      类似这样的东西(假设这是在/googlecallback):

      $client = new Google_Client();
      
      $client->setApplicationName('MyAppName');
      $client->setScopes(array('https://www.googleapis.com/auth/plus.me'));
      $client->setClientId('MyCientID');
      $client->setClientSecret('MyClientSecret');
      $client->setRedirectUri('http://mydomain.com/googlecallback');
      $client->setApprovalPrompt('force');
      $client->setAccessType('offline');
      $client->setDeveloperKey('MyDeveloperKey');
      
      if (empty($_GET['code'])) {
          header('Location: '.$client->createAuthUrl());
      } else {
          $client->authenticate($_GET['code']);
          $access_token = $client->getAccessToken();
          // save the token somewhere so you can use later
          // without having to go to the auth page again
      }
      

      【讨论】:

      • 我已经这样做了,但这给了我一个 1 小时的访问令牌,我想要一个永久的访问令牌...但是谢谢你太棒了!
      • 好吧,对不起。但是没有永久令牌,第一次拿到令牌+刷新令牌。然后,新库 (1.x) 会在令牌过期时自动为您刷新令牌。 developers.google.com/accounts/docs/OAuth2WebServer
      • 你有例子吗? x)
      • 没有额外的代码,我发布的那个应该可以正常工作。用户第一次授权您的应用时,他的访问令牌将包含一个“refresh_token”字段。你不需要做任何事情,lib会处理它。如果您没有获得刷新令牌,请检查此问题stackoverflow.com/questions/10827920/…
      • getAccessToken 输出:{"access_token":"random","token_type":"Bearer","expires_in":3600,"id_token":"random","re​​fresh_token":"random", "created":1394292095} 但它会在 1 小时后过期!我调用 setAccessToken() 来使用 API 谢谢谢谢!!
      猜你喜欢
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      相关资源
      最近更新 更多