Google API PHP 简单查询示例权限不足
Google API PHP simple-query example insufficient permission
我正在尝试 运行 来自此处的 Google API PHP 简单查询示例:https://github.com/google/google-api-php-client/blob/master/examples/service-account.php
我的目标是测试 OAuth2 并在我的 Web 应用程序和共享给我的 google 电子表格之间创建服务器到服务器的连接。
这是我拥有的:
$client_id = 'XXX.apps.googleusercontent.com'; //Client ID
$service_account_name = 'XXX@developer.gserviceaccount.com'; //Email Address
$key_file_location = 'C:/Users/Hp/Downloads/cMessage-638a8a247351.p12';
if ($client_id == '' || !strlen($service_account_name) || !strlen($key_file_location)) {
echo missingServiceAccountDetailsWarning();
}
$client = new Google_Client();
$client->setApplicationName("cMessage");
$service = new Google_Service_Books($client);
/* * **********************************************
If we have an access token, we can carry on.
Otherwise, we'll get one with the help of an
assertion credential. In other examples the list
of scopes was managed by the Client, but here
we have to list them manually. We also supply
the service account
* ********************************************** */
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name, array('https://www.googleapis.com/auth/books'), $key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
/* * **********************************************
We're just going to make the same call as in the
simple query as an example.
* ********************************************** */
$optParams = array('filter' => 'free-ebooks');
$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
echo "<h3>Results Of Call:</h3>";
foreach ($results as $item) {
echo $item['volumeInfo']['title'], "<br /> \n";
}
我尝试 运行 这个,我得到这个:
Error calling GET https://www.googleapis.com/books/v1/volumes?q=Henry+David+Thoreau&filter=free-ebooks: (403) Insufficient Permission
我仔细检查了我的凭据、时区,并在我的开发者控制台中启用了 Google 的图书 API。
我不知道我还缺少什么。请帮忙。谢谢。
好的,我开始工作了!如果有人遇到这个问题,我会这样做:
在 Google 开发人员控制台中,在 APIs & AUTH >> APIs 下,我不得不打开一些 APIs 和 SDK。虽然我不太确定是哪个成功了,但这里是我启用的 APIs 和 SDK:
管理 SDK
BigQuery API
书籍API
Debuglet 控制器API
开车API
驱动SDK
Google 应用市场 SDK
Google云SQL
Google 云存储
Google 云存储 JSON API
Google+ API
我正在尝试 运行 来自此处的 Google API PHP 简单查询示例:https://github.com/google/google-api-php-client/blob/master/examples/service-account.php 我的目标是测试 OAuth2 并在我的 Web 应用程序和共享给我的 google 电子表格之间创建服务器到服务器的连接。
这是我拥有的:
$client_id = 'XXX.apps.googleusercontent.com'; //Client ID
$service_account_name = 'XXX@developer.gserviceaccount.com'; //Email Address
$key_file_location = 'C:/Users/Hp/Downloads/cMessage-638a8a247351.p12';
if ($client_id == '' || !strlen($service_account_name) || !strlen($key_file_location)) {
echo missingServiceAccountDetailsWarning();
}
$client = new Google_Client();
$client->setApplicationName("cMessage");
$service = new Google_Service_Books($client);
/* * **********************************************
If we have an access token, we can carry on.
Otherwise, we'll get one with the help of an
assertion credential. In other examples the list
of scopes was managed by the Client, but here
we have to list them manually. We also supply
the service account
* ********************************************** */
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name, array('https://www.googleapis.com/auth/books'), $key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
/* * **********************************************
We're just going to make the same call as in the
simple query as an example.
* ********************************************** */
$optParams = array('filter' => 'free-ebooks');
$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
echo "<h3>Results Of Call:</h3>";
foreach ($results as $item) {
echo $item['volumeInfo']['title'], "<br /> \n";
}
我尝试 运行 这个,我得到这个:
Error calling GET https://www.googleapis.com/books/v1/volumes?q=Henry+David+Thoreau&filter=free-ebooks: (403) Insufficient Permission
我仔细检查了我的凭据、时区,并在我的开发者控制台中启用了 Google 的图书 API。
我不知道我还缺少什么。请帮忙。谢谢。
好的,我开始工作了!如果有人遇到这个问题,我会这样做:
在 Google 开发人员控制台中,在 APIs & AUTH >> APIs 下,我不得不打开一些 APIs 和 SDK。虽然我不太确定是哪个成功了,但这里是我启用的 APIs 和 SDK:
管理 SDK
BigQuery API
书籍API
Debuglet 控制器API
开车API
驱动SDK
Google 应用市场 SDK
Google云SQL
Google 云存储
Google 云存储 JSON API
Google+ API