【问题标题】:Google API Refresh TokenGoogle API 刷新令牌
【发布时间】:2016-01-26 03:15:46
【问题描述】:

在过去的几个小时里,我一直在尝试从最初的 Google API 调用中打印我的 refresh_token(即使在像其他人建议的那样取消授权和重新授权之后),但我仍然没有任何运气...... API 按预期工作,但我想设置刷新功能,以便我的用户在令牌过期后不会失去访问权限(导致错误)。

目前,以下代码将 $_SESSION['refresh_token'] 设置为 null。

任何帮助都将不胜感激,因为我已经用头敲击键盘很长一段时间了......

private function googleAnalyticsClient(){
    App::import('Vendor', 'google-api-php-client', array('file' => 'autoload.php'));
    $client = new Google_Client();
    $client->setAuthConfigFile(__DIR__ . '/client_secrets.json'); 
    $client->setAccessType('offline');
    $client->setApprovalPrompt('force');
    $client->addScope('https://www.googleapis.com/auth/analytics.readonly');

    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
        $client->setAccessToken($_SESSION['access_token']);
        $analytics = new Google_Service_Analytics($client);
        return $analytics;
    } else {
      $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/dashboard/oauthcallbacks';
      header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
    }
}

public function getOauth()
{
    App::import('Vendor', 'google-api-php-client', array('file' => 'autoload.php'));

    // Create the client object and set the authorization configuration
    // from the client_secrets.json you downloaded from the Developers Console.
    $client = new Google_Client();
    $client->setAuthConfigFile(__DIR__ . '/client_secrets.json');
    $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/dashboard/oauthcallbacks');
    $client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);

    // Handle authorization flow from the server.
    if (! isset($_GET['code'])) {
      $auth_url = $client->createAuthUrl();
      header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
    } else {
      $client->authenticate($_GET['code']);
      $_SESSION['access_token'] = $client->getAccessToken();
      $_SESSION['refresh_token'] = $client->getRefreshToken();
      $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/dashboard/tpstats';
      header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
    }
}

【问题讨论】:

    标签: php oauth-2.0 google-api google-analytics-api google-api-php-client


    【解决方案1】:

    在 getOauth() 函数中添加以下内容

    $client = new Google_Client();
    $client->setAuthConfigFile(__DIR__ . '/client_secrets.json');
    $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/dashboard/oauthcallbacks');
    $client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);    
    $client->setAccessType('offline'); //To fetch refresh token(Refresh token issued only first time)
    $client->setApprovalPrompt('force'); //Adding this will force for refresh token
    

    【讨论】:

    • 通过反复试验,我终于能够让它工作。我只是在 $client->authenticate() 之后 var_dump'd $client...有时你只需要一些睡眠:P 感谢您的帮助
    猜你喜欢
    • 2016-08-03
    • 2020-02-06
    • 2021-05-15
    • 2012-02-15
    • 2014-11-25
    • 2016-12-13
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多