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

前端 未结 1 688
醉梦人生
醉梦人生 2020-12-07 05:18

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

相关标签:
1条回答
  • 2020-12-07 05:39

    This is how , I solved the problem, We need to store the refreshToken in our database and using that refreshToken, We can get another accessToken .which can be used to get the result and it does not need another authentication .

    <?php
            $client = new Google_Client();
            $client->setAuthConfigFile(plugin_dir_url( __FILE__ ) . '/client_secrets.json');
            $client->setRedirectUri( site_url() . '/wp-admin/admin.php?page=analytica-admin-settings');
            $client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
            $client->setIncludeGrantedScopes(true);
            $client->setAccessType('offline');
            if ( isset( $_GET['code'] )) {
    
                if($client->isAccessTokenExpired()){
    
                    $client->authenticate($_GET['code']);
    
                    $accessToken = $client->getAccessToken();
                    $refreshToken = $client->getRefreshToken();
    
                    $analytica_tokens = json_encode( array( 'time' => current_time( 'mysql' ),'accessToken' =>  $accessToken, 'refreshToken' => $refreshToken ) );
                    update_option( 'analytica_tokens', $analytica_tokens );
                } 
            } else {
                $resultset = json_decode(get_option('analytica_tokens'));
    
                if ($client->isAccessTokenExpired()) {
                    if( isset( $resultset ) ){
                        $refreshToken = $resultset->refreshToken;
                        $client->refreshToken( $refreshToken );
                        $accessToken = $client->getAccessToken();           
                        $analytica_tokens = json_encode( array( 'time' => current_time( 'mysql' ), 'accessToken' =>  $accessToken, 'refreshToken' => $refreshToken ) );
                        update_option( 'analytica_tokens', $analytica_tokens );
                    } else {
                        echo 'You need to reauthorize the application to get the analytics report.';
                    }
                }
            }
            $auth_url = $client->createAuthUrl();
            ?>
             <a class="connect-to-google-analytics" href='<?php echo $auth_url; ?>'  id="loginText">Connect To Your Google Analytics Account </a>
            <?php
            if( isset($accessToken) ){
                $_SESSION['access_token'] = $accessToken ? $accessToken : $refreshToken;
                $client->setAccessToken($_SESSION['access_token']);
                // Create an authorized analytics service object.
                $analytics = new Google_Service_Analytics($client);
    
                // Get the first view (profile) id for the authorized user.
                $profile = $this->getFirstProfileId($analytics);
    
                // Get the results from the Core Reporting API and print the results.
                $this->results = $this->getResults($analytics, $profile);
            }
            ?>
    
    0 讨论(0)
提交回复
热议问题