gpt4 book ai didi

php - 使用服务帐户 : { "error" : "access_denied" }. 访问 Google 日历事件 没有谷歌应用程序

转载 作者:行者123 更新时间:2023-12-04 04:15:00 25 4
gpt4 key购买 nike

我想使用服务帐户访问谷歌日历。这是我的代码: <NUMBER>被 Google API 控制台上的正确值所取代。

<?php

require_once 'googleapi/Google_Client.php';
require_once 'googleapi/contrib/Google_CalendarService.php';

const CLIENT_ID = '<NUMBER>.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = '<NUMBER>@developer.gserviceaccount.com';
const MY_EMAIL = '<MY NAME>@gmail.com';
const KEY_FILE = 'privatekey.p12';

$client = new Google_Client();
$client->setClientId(CLIENT_ID);
$client->setApplicationName("<APP NAME>");

$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/calendar'),
$key,
'notasecret',
'http://oauth.net/grant_type/jwt/1.0/bearer',
MY_EMAIL)
);

$cal = new Google_CalendarService($client);
$calList = $cal->calendarList->listCalendarList();

print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";

当我执行我的代码时,我收到:

Fatal error: Uncaught exception 'Google_AuthException' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "access_denied" }'' in /home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/auth/Google_OAuth2.php:279 Stack trace: #0 /home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/auth/Google_OAuth2.php(256): Google_OAuth2->refreshTokenRequest(Array) #1 /home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/auth/Google_OAuth2.php(209): Google_OAuth2->refreshTokenWithAssertion() #2 /home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/service/Google_ServiceResource.php(166): Google_OAuth2->sign(Object(Google_HttpRequest)) #3 /home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/contrib/Google_CalendarService.php(154): Google_ServiceResource->__call('list', Array) #4 /home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/testService.php(32): Google_CalendarListServiceResource->listCalendarList() #5 {main} thrown in /home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/auth/Google_OAuth2.php on line 279

如果我更改我的代码:

$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/calendar'),
$key));

我收到:

(403) Access Not Configured

这里出了什么问题?

最佳答案

我有解决方案。首先好的代码是最后的:

$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/calendar'),
$key));

问题不在于代码,而在于我的谷歌帐户,我在 Google API 控制台中提到了一个 Referer。删除该字段后,代码可以正常工作。

出于信息目的,如果您想访问共享日历,请不要忘记与您的 XXXXXX@developer.gserviceaccount.com(您的 SERVICE_ACCOUNT_NAME)共享您的日历。

有关详细信息,请参阅:

https://groups.google.com/forum/?fromgroups=#!topic/google-ajax-search-api/kaKYuUstwB0/discussion

关于php - 使用服务帐户 : { "error" : "access_denied" }. 访问 Google 日历事件 没有谷歌应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13111396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com