我正在使用谷歌api php客户端来使用我公司的驱动器。
但我有一个问题:当通过ID在驱动器中获取文件的元数据时,代码会生成错误。
{ "error": { "errors": [ { "domain": "global", "reason": "insufficientPermissions", "message": "Insufficient Permission: Request had insufficient authentication scopes." } ], "code": 403, "message": "Insufficient Permission: Request had insufficient authentication scopes." } }
代码示例
<?php
use GoogleClient;
use GoogleServiceDrive;
$client = new Client();
$client->setAuthConfig([
'type' => 'service_account',
'client_id' =>'xxxxxx',
'client_email' => 'xxxxxx',
'private_key_id' => 'xxxxxx',
'private_key' => 'xxxxxx',
]);
$client->setScopes([
'https://www.googleapis.com/auth/drive',
]);
$client->setSubject('xxxxxx');
$drive = new Drive($client);
$fileId = 'xxxxxx';
$optParams = [
'fields' => '*',
];
$results = $drive->files->get($fileId, $optParams); // -> error 403
注意:对于api驱动器的其他方法也是同样的问题。
谢谢!😉
问题是我有另一个代码在之后重写了作用域。
所以我用addScope()
替换了所有代码中的setScopes()
。