Google API - 强制每次都授予权限

时间:2012-06-01 13:38:03

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

我正在使用Google API PHP客户端。每次我尝试登录时,我都被迫授予应用程序权限。

以下是我的代码。我基本上是访问Google API for Analytics

require_once 'lib/apiClient.php';
require_once 'lib/contrib/apiAnalyticsService.php';
session_start();

$client = new apiClient();
$client->setApplicationName("Google Analytics");

$client->setClientId('7xxxx');
$client->setClientSecret('xxxx');
$client->setRedirectUri('xxxx');
$client->setDeveloperKey('xxxx');

$analytics = new apiAnalyticsService($client);

if (isset($_GET['logout'])) {
    unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
    $client->authenticate();
    $_SESSION['token'] = $client->getAccessToken();
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
    $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
    $profileId = getProfileIds($analytics);

    echo "<select>";
    foreach ($profileId as $profiles) {
        echo "<option value=\"" . $profiles['profileId'] . "\">" . $profiles['name'] .
            "</option>";
    }
    echo "</select>";

} else {
    $authUrl = $client->createAuthUrl();
    print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

我们可以将哪个选项传递给 createAuthUrl() ??

1 个答案:

答案 0 :(得分:7)

通过设置

$client->setApprovalPrompt('auto');

如果帐户有权访问api,它会自动重定向。默认情况下,'force'