我正在尝试创建一个应用程序(使用云端硬盘服务帐户)列出给定文件夹中的文件并允许用户搜索这些文件的内容。我得到了403 Insufficient Permissions
我无法解释的错误。
我已经编辑了代码Google API PHP 客户端示例:
$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.";
确切的错误消息($e->getMessage()
in the catch
上)是Error calling GET https://www.googleapis.com/drive/v2/files: (403) Insufficient Permission
- 我认为 /drive 和 /drive.file 范围给了我所需的所有权限?
首先,快速说明:您正在请求Drive
范围和Drive.File
范围。后者是前者的子集,因此无需请求。你应该删除'https://www.googleapis.com/auth/drive.file'
line.
至于权限不足,这可能是由于开发者控制台配置不正确造成的。您应该仔细检查 API 和 SDK 是否已为此特定项目启用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)