Google API权限不足列表文件 [英] Google API Insufficient Permissions listing files
问题描述
我正尝试创建一个应用(使用云端硬盘服务帐户)列出给定文件夹中的文件,并允许用户搜索这些文件的内容。我得到了一个 403权限不足
的错误,我无法解释。
我编辑了 Google API PHP客户端示例:
$ client_id ='[REMOVED]'; //客户ID
$ service_account_name ='[REMOVED]'; //电子邮件地址
$ key_file_location ='key.p12'; //key.p12
$ client = new Google_Client();
$ client-> setApplicationName(Client_Library_Examples);
服务=新Google_Service_Drive($客户);
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/drive',
'https: //www.googleapis.com/auth/drive.file'
),
$ key
);
$ client-> setAssertionCredentials($ cred);
$ b $ if($ client-> getAuth() - > isAccessTokenExpired()){
$ client-> getAuth() - > refreshTokenWithAssertion($ cred);
}
$ _SESSION ['service_token'] = $ client-> getAccessToken();
$ result = array();
$ pageToken = NULL;
do {
try {
$ parameters = array();
if($ pageToken){
$ parameters ['pageToken'] = $ pageToken;
}
$ files = $ service-> files-> listFiles($ parameters);
$ result = array_merge($ result,$ files-> getItems());
$ pageToken = $ files-> getNextPageToken();
} catch(Exception $ e){
echo< br />发生错误:。 $ E->的getMessage();
$ pageToken = NULL;
}
} while($ pageToken);
echo< pre>;
print_r($ result);
echo< / pre>;
echo< br />执行完成。;
确切的错误信息( $ e-> getMessage()上面的
catch
中的code>是调用GET时出错https://www.googleapis.com/drive/v2/files :( 403)权限不足
- 我认为/ drive和/drive.file作用域为我提供了我需要的所有权限?
Drive
作用域和 Drive.File
范围。后者是前者的子集,因此不需要请求它。您应该删除'https://www.googleapis.com/auth/drive.file'
行。 至于权限不足,这可能是由于开发者控制台配置不正确。您应该仔细检查是否为此特定项目启用了API和SDK。
I am trying to create an app that (using a Drive service account) lists files in a given folder and allows users to search the content of those files. I am getting a 403 Insufficient Permissions
error which I cannot explain.
I have edited the code from the Google API PHP Client Example:
$client_id = '[REMOVED]'; //Client ID
$service_account_name = '[REMOVED]'; //Email Address
$key_file_location = 'key.p12'; //key.p12
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
service = new Google_Service_Drive($client);
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/drive',
'https://www.googleapis.com/auth/drive.file'
),
$key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
$result = array();
$pageToken = NULL;
do {
try {
$parameters = array();
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$result = array_merge($result, $files->getItems());
$pageToken = $files->getNextPageToken();
} catch (Exception $e) {
echo "<br/>An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
echo "<pre>";
print_r($result);
echo "</pre>";
echo "<br />Execution completed.";
The exact error message ($e->getMessage()
in the catch
above) is Error calling GET https://www.googleapis.com/drive/v2/files: (403) Insufficient Permission
- I thought the /drive and /drive.file scopes gave me all the permissions I needed?
First, a quick note: You are requesting both the Drive
scope and the Drive.File
scope. The later is a subset of the former, so there is no need to request it. You should remove 'https://www.googleapis.com/auth/drive.file'
line.
As to the insufficient permissions, this is possibility due to incorrect Developer Console configuration. You should double check that both the API and SDK are enabled for this particular project.
这篇关于Google API权限不足列表文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!