您能帮我吗?
我正在使用PHP GOOGLE API V2.2.2
我的代码使用为其创建的gserviceaccount起作用,但是,当我尝试操作个人GOOGLE DRIVE上的文件时,出现此错误...
我尝试按照此处显示的步骤操作:Github issue 801
一切正常,我的项目显示在我的authorized client list
上这是我的代码,用于在Google驱动器中创建一个文件夹:
function NovaPasta() {
require_once APPPATH . 'third_party/google-api-php-client-2.2.2/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS='. APPPATH . 'third_party/google-api-php-client-2.2.2/Asist-428f3540df9a.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
//$client->setAuthConfig(APPPATH . 'third_party/google-api-php-client-2.2.2/Asist-428f3540df9a.json');
$client->addScope("https://www.googleapis.com/auth/drive");
$client->setSubject('xxxx@gmail.com');
$driveService = new Google_Service_Drive($client);
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => 'new folder name',
'mimeType' => 'application/vnd.google-apps.folder'));
$file = $driveService->files->create($fileMetadata, array(
'fields' => 'id'));
printf("Folder ID: %s\n", $file->id);
}
这是错误:
遇到未捕获的异常 类型:Google_Service_Exception
消息:{“ error”:“ unauthorized_client”,“ error_description”:“未授权客户端使用此方法检索访问令牌,或者未授权客户端进行任何请求的作用域。” }
答案 0 :(得分:0)
如果要使用服务帐户,则取消注释$ client-> setAuthConfig指向json文件并删除行$ client-> useApplicationDefaultCredentials();
在https://github.com/googleapis/google-api-php-client/tree/master/examples上查看更多示例
$client = new Google_Client();
$client->setAuthConfig('service-account.json');
$client->setScopes(['https://www.googleapis.com/auth/drive']);
$service = new Google_Service_Drive($client);
$fileMetadata = new Google_Service_Drive_DriveFile(array('name' => 'Google_Service_Folder','mimeType' => 'application/vnd.google-apps.folder'));
$file = $service->files->create($fileMetadata, array('fields' => 'id'));