我目前正在使用Google日历API开发一个网页应用。然而,每隔一小时,我都会被提示要验证快速启动访问权限链接。有人知道如何解决这个问题吗?
细节如下:
- 我创建了一个新的gmail id:redu@gmail.com
- redu@gmail.com关联了一个日历
- 我的基于PHP的网页应用需要对日历进行以下操作:
- 为每个注册用户创建一个新的日历(作为redu@gmail.com的附加日历)
- 为已登录用户创建一个事件,并添加另一个注册用户作为受邀者
我已经尝试使用OAuth和服务帐户,但没有成功。非常感谢任何帮助。
下面是使用服务帐户凭据创建Google_Client和Service对象的代码:
function __construct()
{
Service account based client creation.
$this->client = new Google_Client();
$this->client->setApplicationName("Redu");
$this->client->setAuthConfig(CREDENTIALS_PATH);
$this->client->setScopes([SCOPES]);
$this->client->setSubject('redu@gmail.com');
$this->client->setAccessType('offline');
$this->service = new Google_Service_Calendar($this->client);
}
当我试图使用 $service 对象创建日历或创建事件时,会出现一个错误,提示未设置域宽权限。然而,当我创建服务账号时确实启用了域宽委派。
编辑:
以下是我的代码,使用服务账号密钥创建 Google_Client 并使用客户端为 redu@gmail.com 创建新的日历。请注意,我已经将 redu@gmail.com 的日历与 reduservice@subtle-breaker-280602.iam.gserviceaccount.com 共享,并将权限设置为“管理更改和管理共享”。下面是我收到的错误:
require (__DIR__.'/../../../vendor/autoload.php');
define('CREDENTIALS_PATH', __DIR__ . '/redu_service_account_credentials.json');
define('SCOPES', Google_Service_Calendar::CALENDAR);
function createNewCalendar($userName) {
//Service account based client creation.
$client = new Google_Client();
$client->setApplicationName("REdu");
// path to the credentials file obtained upon creating key for service account
$client->setAuthConfig(CREDENTIALS_PATH);
$client->setScopes([SCOPES]);
$client->setSubject('redu@gmail.com');
$client->setAccessType('offline');
$service = new Google_Service_Calendar($client);
$calendar = new Google_Service_Calendar_Calendar();
$calendar->setSummary($userName);
$calendar->setTimeZone('America/Los_Angeles');
$createdCalendar = $service->calendars->insert($calendar);
// Make the newly created calendar public
$rule = new Google_Service_Calendar_AclRule();
$scope = new Google_Service_Calendar_AclRuleScope();
$scope->setType("default");
$scope->setValue("");
$rule->setScope($scope);
$rule->setRole("reader");
// Make the calendar public
$createdRule = $service->acl->insert($createdCalendar->getId(), $rule);
return $createdCalendar->getId();
}
错误:
Fatal error: Uncaught exception 'Google_Service_Exception' with message '{
"error": "unauthorized_client",
"error_description": "Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested."
}'