温馨提示:本文翻译自stackoverflow.com,查看原文请点击:php - Google Calendar API returning unauthorized even when domain is properly registered
google-api-php-client google-calendar-api php push-notification

php - 即使已正确注册域,Google Calendar API仍会返回未经授权的状态

发布于 2020-04-20 17:11:47

我正在使用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"
  }
}

查看更多

提问者
Boefjim
被浏览
16
DaImTo 2020-02-05 22:09

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);