小编glo*_*bal的帖子

Google Admin SDK:您无权访问此API

由于自上周以来我一直禁用Google登录身份验证,因此我试图让oAuth 2.0使用服务帐户.我们希望为我们的内部Web应用程序上的用户提供在办公室外设置的机会.

我下载了最新的用于PHP的Google API客户端库.在Google Developer Console中,我为我的应用程序创建了一个新项目并创建了一个Service account凭据.我还启用了API服务:Admin SDK在Developer Console中.

在此输入图像描述

我已授予帐户用户ID访问权限的范围(我认为): 在此输入图像描述

当我使用service-account.php示例并更改详细信息时,我收到带有访问令牌的JSON,但是当我执行CURL请求(与之前相同)以从用户获取电子邮件设置时,"You are not authorized to access this API."会发生错误.

我的代码:

<?php

include_once "templates/base.php";
require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
$client_id = '124331845-DELETEDPART-hbh89pbgl20citf6ko.apps.googleusercontent.com'; //Client ID
$service_account_name = '124331845-DELETEDPART-89pbgl20citf6ko@developer.gserviceaccount.com'; //Email Address
$key_file_location = 'globaltext-4ce09b20cb73.p12'; //key.p12

$client = new Google_Client();
if (isset($_SESSION['service_token'])) {
  $client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://apps-apis.google.com/a/feeds/emailsettings/2.0/'),
    $key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}

$aOutput = json_decode($client->getAccessToken());

$strEmailAdresSplit …
Run Code Online (Sandbox Code Playgroud)

php google-api google-oauth google-api-php-client google-admin-sdk

5
推荐指数
1
解决办法
1199
查看次数