我正在尝试通过 PHP 为一个特定用户创建日历事件 - 例如 development@example.com。
我在 Google Developers Console 中创建了服务帐户,获得了 ClientID、电子邮件地址和私钥。身份验证通过代码完成:
$client = new Google_Client();
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$cred = new Google_Auth_AssertionCredentials(
'somelongstring@developer.gserviceaccount.com.',
array('https://www.googleapis.com/auth/calendar','https://www.googleapis.com/auth/calendar.readonly'),
file_get_contents('p12 file'));
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
这种类型的身份验证似乎还不错。但是所有事件都是作为用户创建的,其电子邮件地址为 somelongstring@developer.gserviceaccount.com 而不是 development@example.com。
我试过设置子参数:
$cred = new Google_Auth_AssertionCredentials(
....
$cred->sub = 'developement@example.com';
$client->setAssertionCredentials($cred);
但是这段代码抛出异常:
Google_Auth_Exception: Error refreshing the OAuth2 token, message: '{ "error" : "access_denied", "error_description" : "Requested client not authorized." }'
现在我迷路了。有什么建议吗?