强制授予权限每次

强制授予权限每次

本文介绍了Google API - 强制授予权限每次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google API PHP Client。每次我尝试登录时,我都被强制授予应用程序权限。

以下是我的代码。我基本上访问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>;
}
回显< / select>;

} else {
$ authUrl = $ client-> createAuthUrl();
print< a class ='login'href ='$ authUrl'>连接我!< / a>;
}

我们可以传递给 createAuthUrl()

解决方案

通过设置

  $客户端 - > setApprovalPrompt( '自动'); 

如果帐户可以访问apis,它将自动重定向。默认情况下,它的'force'


I am using Google API PHP Client. And every time i try to login I am forced to grant permissions to the apps.

Below is my code. I am basically accessing 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>";
}

What option can we pass to createAuthUrl() ??

解决方案

By Setting

$client->setApprovalPrompt('auto');

It will automatically redirect if the account has access to the apis. By Default its 'force'

这篇关于Google API - 强制授予权限每次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 03:53