OAuth.io服务器端没有';t刷新Google令牌


OAuth.io server-side doesn't refresh Google token

首先请不要将其标记为重复,因为这与服务器端(PHP)有关,而不是与其他帖子一样与客户端有关。

我正在尝试通过Oauth.io刷新Google API的令牌。我关注了这篇文章和许多其他帖子,但没有运气。遗憾的是,Oauth.io文档并不是最好的。我对推特和脸书没有这个问题,只有谷歌。

当我第一次连接时,我得到了refresh_token。然后我需要每天做一次API调用。

{
  "access_token":"xxx",
  "token_type":"Bearer",
  "expires_in":3600,
  "refresh_token":"xxx",
  "id_token":"xxx",
  "provider":"google_analytics"
}

问题是如何通过Oauth.io刷新Google代币?

文件上写着:

//如果需要,auth方法会自动刷新令牌$request_object=$oauth->auth("facebook",数组('credentials'=>$credentials));

并指出了这一点,但这并不能解决问题。它所做的只是我得到了refresh_token值作为响应。

更新

根据这个帖子,我试图做:

$request_object->post('https://oauth.io/auth/access_token', array(
    'code' => 'xxx', // here I tried access_token, refresh_token
    'key' => 'xxx',
    'secret' => 'xxx',
));

但我得到的只是

array(4) {
  'status' =>
      string(5) "error"
  'code' =>
      int(401)
  'message' =>
      string(70) "Bearer token invalid. Follow the oauth2-token link to get a valid one!"
  'data' =>
      array(1) {
          'code' =>
              string(17) "UnauthorizedError"
      }
}

还是什么都没有。

终于找到了。您可以使用:刷新access_token

    $this->oauth = new OAuth();
    $this->oauth->initialize($publicKey, $secretKey);
  1. $refreshedCredentials=$this->oauth->refreshCredentials($oldCredentials,true)

  1. $request_object = $oauth->auth('google_analytics', array( 'credentials' => $credentials, 'force_refresh' => true ));

这两个答案都可以在这里找到。我把它放在面前一整天都没有看到。

我更喜欢使用第一个解决方案,因为我想保存新的access_token,以便以后可以重用它。您需要记住,您需要传递refresh_token,这是您在第一次访问时获得的,以及最后一次访问时得到的,以获得刷新的访问权限-refresh_taken在用户撤销访问权限之前不会更改。