Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

php - How to use refresh token to get authorized in background and getting access token?

I am trying to get accessToken using refreshToken, below I have posted my code please someone guide me.

It is from a wordpress plugin I am developing, I only need to retrieve pageViews and pagePath so not preferring using a available plugin.

Taken reference from Use OAuth Refresh Token to Obtain New Access Token - Google API

 if( isset( $this->options['authenication_code'] ) ){   //plugin setting page settings 
                global $wpdb;
                $resultset = $wpdb->get_row( 'SELECT `refreshToken` FROM ' . $wpdb->prefix . 'analyticaAnalytics WHERE authenication_code ="' . $this->options["authenication_code"] . '"', ARRAY_A );
                var_dump( $resultset['refreshToken'] ); //retrieved refreshToken from database
                if ($client->isAccessTokenExpired()) {              //boolean true
                    $client->refreshToken( $resultset['refreshToken'] ); 
                    var_dump( $client );//getting blank
                }
            }

../google-api-php-client/src/Google/Auth/oauth2.php

private function refreshTokenRequest($params)
  { 
    if (isset($params['assertion'])) {

      $this->client->getLogger()->info(
          'OAuth2 access token refresh with Signed JWT assertion grants.'
      );
    } else {
        $this->client->getLogger()->info('OAuth2 access token refresh');
    }

    $http = new Google_Http_Request(
        self::OAUTH2_TOKEN_URI,
        'POST',
        array(),
        $params
    );

    $http->disableGzip();
    $request = $this->client->getIo()->makeRequest($http);
    //var_dump( $request );exit;//response 400, invalid grant
    $code = $request->getResponseHttpCode();

    $body = $request->getResponseBody();
    if (200 == $code) {
      $token = json_decode($body, true);
      if ($token == null) {
        throw new Google_Auth_Exception("Could not json decode the access token");
      }

      if (! isset($token['access_token']) || ! isset($token['expires_in'])) {
        throw new Google_Auth_Exception("Invalid token format");
      }

      if (isset($token['id_token'])) {
        $this->token['id_token'] = $token['id_token'];
      }
      $this->token['access_token'] = $token['access_token'];
      $this->token['expires_in'] = $token['expires_in'];
      $this->token['created'] = time();
    } else {
      throw new Google_Auth_Exception("Error refreshing the OAuth2 token, message: '$body'", $code);
    }
  }

After spending a lot of time I got the error is responsecode 400 for $request = $this->client->getIo()->makeRequest($http); and that is invalid grant.

full code

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...