【问题标题】:OAuth.io server-side doesn't refresh Google tokenOAuth.io 服务器端不刷新 Google 令牌
【发布时间】:2016-09-16 04:45:15
【问题描述】:

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

我正在尝试通过 Oauth.io 刷新 Google API 的令牌。我关注了this 和许多其他帖子,但没有运气。不幸的是 Oauth.io 文档不是最好的。我在 Twitter 和 Facebook 上没有这个问题,只有 Google。

我在第一次连接时获得了 refresh_token。然后我需要每天调用一次 API。

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

问题是如何通过 Oauth.io 刷新 Google 令牌?

documentation 说:

// auth 方法根据需要自动刷新令牌 $request_object = $oauth->auth('facebook', array( '凭证' => $凭证 ));

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

更新

根据this的帖子我尝试过:

$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"
      }
}

还是什么都没有。

【问题讨论】:

    标签: php google-api oauth.io


    【解决方案1】:

    终于找到了。您可以使用以下方法刷新 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 ));

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

    我更喜欢使用第一种解决方案,因为我想保存新的 access_token 以便以后重复使用。并且您需要记住,您需要通过第一个 access_token 获得的 refresh_token 以及最后一个 access_token 来获得刷新 access_token - refresh_token 在用户撤销访问权限之前不会更改。

    【讨论】:

      猜你喜欢
      • 2014-07-08
      • 2014-02-08
      • 2015-09-18
      • 2012-04-18
      • 1970-01-01
      • 2021-08-26
      • 1970-01-01
      • 2022-11-09
      • 2019-03-26
      相关资源
      最近更新 更多