我是Windows / Active Directory架构师,在一家小型公司工作有25年的经验。我正在尝试开发一种方法来管理多个不同的身份验证系统。其中之一是企业gmail帐户。我需要能够使用Google的管理API读写Gmail。我用PHP编写代码,并将mySQL用于数据库后端。我的问题是我无法获取php脚本来连接到Google。它不断产生相同的错误:
PHP Fatal error: Uncaught Google_Service_Exception: {
"error": {
"errors": [
{
"domain": "global",
"reason": "forbidden",
"message": "Not Authorized to access this resource/api"
}
],
"code": 403,
"message": "Not Authorized to access this resource/api"
}
}
in D:\Inetpub\WWWRoot\datasync\src\Google\Http\REST.php:118
Stack trace:
#0 D:\Inetpub\WWWRoot\datasync\src\Google\Http\REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#1 D:\Inetpub\WWWRoot\datasync\src\Google\Task\Runner.php(176): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#2 D:\Inetpub\WWWRoot\datasync\src\Google\Http\REST.php(58): Google_Task_Runner->run()
#3 D:\Inetpub\WWWRoot\datasync\src\Google\Client.php(798): Google_Http_REST::execute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...', Array)
#4 D:\Inetpub\WWWRoot\datasync\src\Google\Service\Resource.ph in D:\Inetpub\WWWRoot\datasync\src\Google\Http\REST.php on line 118
该脚本使用服务帐户进行连接,并使用json凭证文件。我已确保服务帐户可以通过管理控制台/安全性/管理API客户端访问权限进行访问:
102632896707704106543 View and manage the provisioning of groups on your domain https://www.googleapis.com/auth/admin.directory.group
View and manage the provisioning of users on your domain https://www.googleapis.com/auth/admin.directory.user
我还确保服务帐户已通过域范围的授权激活。
我已经在网上搜索了所有的解决方案。
这是我的生成错误的测试脚本:
<?php
require_once __DIR__ . '/vendor/autoload.php';
$credentials_file = './redacted.json';
$client = new \Google_Client();
$client->setAuthConfig($credentials_file);
$client->setApplicationName("webadmin");
$client->setSubject("datasync@redacted.iam.gserviceaccount.com");
$client->setScopes([
'https://www.googleapis.com/auth/admin.directory.user',
'https://www.googleapis.com/auth/admin.directory.user.readonly'
]);
$service = new \Google_Service_Directory($client);
echo realpath(dirname(__DIR__));
$userKey = 'cperrine@redacted.com';
$results = $service->users->get($userKey);
var_dump($results);
?>
我丢失了一些东西,但我不知道。我已经为此工作了一个星期,我似乎无法摆脱它。任何见识将不胜感激。
答案 0 :(得分:0)
我认为您缺少用户身份验证步骤。
{p}文件中包含的凭据授权由您的应用或Google Dev Console中命名的项目提出的请求。但是,当您要创建对某人数据(例如邮件)的访问权限时,您需要验证用户身份。我建议使用oauth 2 tech,这将允许您获取用户访问令牌(整个过程并不复杂,但是非常安全,可以让您向用户询问其帐户的不同权限)
拥有用户访问令牌后,您可以在Google_Client中进行设置(如我所见,您正在使用官方的Google PHP库)
$credentials_file = './redacted.json';
此后,对API的任何调用均应代表令牌所属的用户。在我的示例中,我在Google Analytics(分析)中使用了它
$client -> refreshToken( $refresh_token );