Google PHP客户端API:权限不足 [英] Google PHP Client API: Insufficient Permission
本文介绍了Google PHP客户端API:权限不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有使用Google PHP客户端API来使用云端硬盘服务的PHP代码.
I have PHP code that use Google PHP Client API to use Drive service.
<?php
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';
const CLIENT_ID = '[MYCLIENTID]';
const SERVICE_ACCOUNT_NAME = '[MYSERVICEACCOUNID]';
// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = './[MYPRIVATEKEY].p12';
// Load the key in PKCS 12 format (you need to download this from the
// Google API Console when the service account was created.
$client = new Google_Client();
$client->setApplicationName("Google Drive Sample");
$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_Auth_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/drive'),
$key)
);
$client->setClientId(CLIENT_ID);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
// Get the json encoded access token.
$token = $client->getAccessToken();
echo "<pre>";
$service = new Google_Service_Drive($client);
$service->apps->listApps();
echo "</pre>";
?>
我已经正确安装了CLIENT_ID,SERVICE_ACCOUNT_NAME和KEY_FILE.当我运行代码时,出现以下错误消息:
I had CLIENT_ID, SERVICE_ACCOUNT_NAME and KEY_FILE correctly installed. When I run the code I get the following error messages:
Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/drive/v2/apps: (403) Insufficient Permission' in C:\xampp\htdocs\oauth\Google\Http\REST.php:79
Stack trace:
#0 C:\xampp\htdocs\oauth\Google\Http\REST.php(44): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request))
#1 C:\xampp\htdocs\oauth\Google\Client.php(499): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))
#2 C:\xampp\htdocs\oauth\Google\Service\Resource.php(195): Google_Client->execute(Object(Google_Http_Request))
#3 C:\xampp\htdocs\oauth\Google\Service\Drive.php(1281): Google_Service_Resource->call('list', Array, 'Google_Service_...')
#4 C:\xampp\htdocs\oauth\drive_client.php(41): Google_Service_Drive_Apps_Resource->listApps()
#5 {main}
thrown in C:\xampp\htdocs\oauth\Google\Http\REST.php on line 79
代码可能有什么问题?
推荐答案
apps#list 方法需要使用范围https://www.googleapis.com/auth/drive.apps.readonly
的授权.只需将其添加到范围数组即可:
The apps#list method requires authorization with the scope https://www.googleapis.com/auth/drive.apps.readonly
. Just add it to the array of scopes:
$client->setAssertionCredentials(new Google_Auth_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.apps.readonly'),
$key)
);
这篇关于Google PHP客户端API:权限不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文