首页 > 解决方案 > Google API 文件创建权限

问题描述

嗨,我正在通过 PHP SDK 创建一个新的 google 驱动器文件,如下所示:

$driveService = new Google_Service_Drive($client);

$fileMetadata = new Google_Service_Drive_DriveFile(array(
        'name' => $filename,
    'parents' => array('root')));
$content = file_get_contents($filepath.'.csv');
$file = $driveService->files->create($fileMetadata, array(
'data' => $content,
'mimeType' => 'text/csv',
'uploadType' => 'media'));

并设置它的权限。

$user_email = $driveService->about->get(array('fields' => 'user'))->getUser()->getemailAddress();
$userPermission = new Google_Service_Drive_Permission(array(
'type' => 'user',
'role' => 'writer',
'emailAddress' => $user_email
));
$fileMetadata = new Google_Service_Drive_DriveFile(array(
        'name' => $filename,
    'parents' => array('root')));
$content = file_get_contents($filepath.'.csv');

当我尝试访问 google drive gui 中的文件时,我可以看到这些文件,但是当我尝试打开/下载它们时,出现授权错误:

 Access to doc-0g-0c-docs.googleusercontent.com was denied

在下载或

 Authorisation requeired

如果我尝试在谷歌记事本中打开。我也无法在谷歌文档中打开它们。

客户端设置代码:

$client = new Google_Client();

    if (!empty(get_option('access_token')) && !$reset) {
    $credentials_file = plugin_dir_path(__FILE__) . 
    get_option('credentials_filename');

    $client->setAuthConfig($credentials_file);
    $client->setPrompt('select_account consent');
    $client->setApplicationName("Client_Library_Examples");

    $client->addScope(Google_Service_Drive::DRIVE);

    $client->setRedirectUri( plugin_dir_url( __FILE__ ) . '/oauth2callback.php');
    $client->setAccessType('offline');        // offline access
    $client->setIncludeGrantedScopes(true);   
    $client->setAccessToken(get_option('access_token'));

标签: phpgoogle-apigoogle-drive-api

解决方案


推荐阅读