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

标签 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() ??

最佳答案

通过设置

$client->setApprovalPrompt('auto');

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

关于php - Google API - 每次都强制授予权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10851358/

相关文章:

php - 使用mysql实时双向同步动态数据哪种方式最好

php - Apache 错误 : cannot load mod_access_compat. 所以

android - Google api gettoken(Context,String,String) 现已弃用

google-analytics - Google Analytics 获取未设置自定义维度的记录

php - 如何让 PhpStorm 建议/自动完成数组键

php - 如何在 Laravel-5 中构建子查询

android - 如何正确删除 Google Apis 控制台项目?

java - Spring3 的 Google 日历 API 库?

google-analytics - 为什么Google Analytics(分析)会在这么多的值(而不是键)前加上“ga:”?

php - 使用 Google Analytics 或自定义解决方案跟踪页面浏览量和事件?