403:在google drive api php中创建文件夹时权限不足

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了403:在google drive api php中创建文件夹时权限不足相关的知识,希望对你有一定的参考价值。

我正在实施google drive api以在我的应用程序中实现。我从google-drive-client-php文档中完成了所有代码配置。但我得到了这个权限错误。请给我任何暗示:

$client = new Google_Client();
    $client->setAuthConfig('client_secrets.json');
    $client->setAccessType("offline");

    $client->setScopes("https://www.googleapis.com/auth/drive");

    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
        $client->setAccessToken($_SESSION['access_token']);
        $drive = new Google_Service_Drive($client);
        $fileMetaData = new Google_Service_Drive_DriveFile(array(
            'name' => 'RootFolder',
            'mimeType' => 'application/vnd.google-apps.folder'));

        $parentFolder = $drive->files->create($fileMetaData, array(
            'fields' => 'id'
        ));

        $permission = new Google_Service_Drive_Permission();
        $permission->setValue('me');
        $permission->setType('anyone');
        $permission->setRole('writer');

        $drive->permissions->insert($parentFolder->getId(), $permission);
        echo "<pre>";
        echo json_encode($parentFolder);

    } else {
        $redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/callback.php';
        header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

感谢你 :)

答案

所以你要求......

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

而你实际上可能需要......

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

供参考,这是API范围的映射:

/** View and manage the files in your Google Drive. */
const DRIVE = "https://www.googleapis.com/auth/drive";

/** View and manage its own configuration data in your Google Drive. */
const DRIVE_APPDATA = "https://www.googleapis.com/auth/drive.appdata";

/** View and manage Google Drive files and folders that you have opened or created with this app. */
const DRIVE_FILE = "https://www.googleapis.com/auth/drive.file";

/** View and manage metadata of files in your Google Drive. */
const DRIVE_METADATA = "https://www.googleapis.com/auth/drive.metadata";

/** View metadata for files in your Google Drive. */
const DRIVE_METADATA_READONLY = "https://www.googleapis.com/auth/drive.metadata.readonly";

/** View the photos, videos and albums in your Google Photos. */
const DRIVE_PHOTOS_READONLY = "https://www.googleapis.com/auth/drive.photos.readonly";

/** View the files in your Google Drive. */
const DRIVE_READONLY = "https://www.googleapis.com/auth/drive.readonly";

/** Modify your Google Apps Script scripts' behavior. */
const DRIVE_SCRIPTS = "https://www.googleapis.com/auth/drive.scripts";
另一答案

我找到了解决问题的方法

去掉

$ permission->的setValue( '我');

并将此Permission方法更改为

$ drive-> permissions-> insert($ parentFolder-> getId(),$ permission);

$ drive-> files-> create($ fileMetaData,array('fields'=>'id'));

在谷歌驱动器api插件中,不推荐使用setValue()方法,因此无效。

以上是关于403:在google drive api php中创建文件夹时权限不足的主要内容,如果未能解决你的问题,请参考以下文章

使用PHP无需用户交互即可连接到Google Drive API

未经PHP身份验证,Google Drive API不允许上传文件

使用服务帐户上传 Google Drive 总是返回 403(存储配额)

Google Drive Api PHP 分段上传空内容

在 Laravel 中使用 Google Drive API

文件移动 Google Drive API v3 PHP