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

倖福魔咒の 提交于 2019-12-13 07:21:37

问题


At first please do not mark this as duplicated as this is related to server-side (PHP) and not to client-side as other posts.

I'm trying to refresh the token for Google API through Oauth.io. I followed this and many other posts but no luck. Unfortunately Oauth.io documentation is not the best one. I don't have this problem with Twitter and Facebook only Google.

I'm getting refresh_token when I connect for the first time. Then I need to do API call once a day.

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

The question is how to refresh Google token through Oauth.io?

The documentation says:

// The auth method automatically refreshes the tokens if needed $request_object = $oauth->auth('facebook', array( 'credentials' => $credentials ));

and points here, but it doesn't solve the problem. All it does is that I'm getting refresh_token value in response.

UPDATE

According to this post I tried to do:

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

but all I'm getting is

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

Still nothing.


回答1:


Finally I found it. You can refresh the access_token using:

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

or

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

Both answers can be found here. I had it in front of all day long and did not see it.

I prefer using 1st solution as I want to save new access_token so I can reuse it later. And you need to remember that you need to pass refresh_token, which you got with first access_token, along with last access_token to get refreshed access_token - refresh_token doesn't change until user revoke access.



来源:https://stackoverflow.com/questions/37320923/oauth-io-server-side-doesnt-refresh-google-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!