我正在尝试创建一个应用程序(使用Drive服务帐户)列出给定文件夹中的文件,并允许用户搜索这些文件的内容。我收到403 Insufficient Permissions
错误,我无法解释。
我编辑了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.";
确切的错误消息($e->getMessage()
中的catch
)是Error calling GET https://www.googleapis.com/drive/v2/files: (403) Insufficient Permission
- 我认为/ drive和/drive.file范围为我提供了所需的所有权限?
答案 0 :(得分:2)
首先,快速说明:您正在请求Drive
范围和Drive.File
范围。后者是前者的子集,因此无需请求它。您应该删除'https://www.googleapis.com/auth/drive.file'
行。
对于权限不足,这可能是由于Developer Console配置不正确造成的。您应该仔细检查是否为此特定项目启用了API和SDK。
答案 1 :(得分:0)
我有同样的问题。我在Google Developer Console中创建了一个新秘密并重新进行了身份验证。这解决了这个问题。