【发布时间】:2016-08-21 04:20:09
【问题描述】:
我在通过 composer 安装的 google apiclient 2.0.0 连接时遇到问题。
这是我到目前为止所做的:
我通过 composer 安装了 apiclient。
我转到 https://console.cloud.google.com/apis/credentials?my-project 并导航到 API 管理器 > 凭据页面。
我为服务帐户创建了凭据并下载了 json。然后我将 json 上传到我的服务器。
我通过管理我的服务帐户为该帐户启用了域范围委派。
我在“域验证”标签下验证了我的域名。
接下来我导航到我的日历并与与我的服务帐户关联的电子邮件地址共享日历,授予管理访问权限。
然后我与服务帐户的电子邮件地址共享了我的日历。
现在我正在尝试使用 api 连接并遇到授权问题。
$json_file = '/path/to/service-account-credentials.json';
$scopes = [
Google_Service_Calendar::CALENDAR,
Google_Service_Calendar::CALENDAR_READONLY
];
// create a new client and authorize
$client = new Google_Client();
// set the basic information of the client
$client->setApplicationName("Apointments");
$client->setSubject( email-address-to-masquerade-as@mycompany.com );
$client->setAccessType('offline');
// load the json credential file
$client->setAuthConfig( $json_file );
$client->setScopes( $scopes );
// check to see if the token is stored in the session
if( isset( $_SESSION['service_token'] ) )
{
// token was stored, use it with the cilent
$client->setAccessToken( $_SESSION['service_token'] );
}
// test the authorization to see if it is expired
if( $client->isAccessTokenExpired() )
{
// Failed authorization, get a new token
$client->refreshTokenWithAssertion();
}
// store the token in the session
$_SESSION['service_token'] = $client->getAccessToken();
从堆栈跟踪中,我发现有问题的代码行是:
$client->refreshTokenWithAssertion();
这会产生错误消息:
客户端错误响应 [url] https://www.googleapis.com/oauth2/v4/token [状态码] 401 [原因短语] Unauthorized
关于我哪里出错了有什么想法吗?
提前致谢, 亚当
【问题讨论】:
标签: php google-calendar-api google-api-php-client service-accounts