我正在使用google php客户端库来创建到我的域的webhook,但是API不断返回未经授权的内容。
我已经成功地跟着上“注册域名”的步骤推动和我的域名列在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);
我已经在渠道创建中添加了客户端,如下所示:
$channel = new Google_Service_Calendar_Channel($googleCalendar->getClient());
但是我仍然遇到相同的错误看来我必须在Google Developer Console上创建一个OAuth2客户端ID,然后使用该ID对我的应用进行身份验证。之后,使用客户端可以使其完美运行,感谢您的帮助!