我正在使用google php客户端库来创建到我的域的Webhook,但是API仍会返回未经授权的内容。
我已成功执行push上有关“注册您的域”的步骤,并且我的域已在API控制台上列出。
根据API和服务>域验证,它在“允许的域”下列出了我的域。
但是它仍然告诉我该域是未经授权的:
库函数
$channel = new Google_Service_Calendar_Channel();
$channel->setId($channelId);
$channel->setType("web_hook");
$channel->setAddress("https://example.com/notifications");
$service->events->watch($calendarId, $channel);
错误消息
{ "error":
{ "errors": [{
"domain": "global",
"reason": "push.webhookUrlUnauthorized",
"message": "Unauthorized WebHook callback channel: https://example.com/notifications"
}],
"code": 401,
"message": "Unauthorized WebHook callback channel: https://example.com/notifications"
}
}
Google_Service_Calendar_Channel要求您发送客户端以对其进行身份验证。我个人将使用服务帐户。
function getServiceAccountClient() {
try {
// Create and configure a new client object.
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/calendar');
return $client;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
$client = getServiceAccountClient();
$channel = new Google_Service_Calendar_Channel($client);
$channel->setId($channelId);
$channel->setType("web_hook");
$channel->setAddress("https://example.com/notifications");
$service->events->watch($calendarId, $channel);