php - 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'));
解决方案
推荐阅读
- reactjs - react-select typescript 问题-通用类型'ValueType'需要2个类型参数.ts(2314)
- c - 即使我尝试在 C 中创建文件也找不到文件时出现分段错误
- c# - JArrary 从键中查找值
- css - 如何更换aa
仅使用 CSS 的元素(其他方法不起作用)
- java - 当我们已经有一个参数化的构造函数时,为什么要在这里写默认构造函数?
- reactjs - 如何从多个不同的组件更新单个 React 组件?
- sql - 在 SQL 中执行减法/寻找平衡
- javascript - Vuejs:从组件观察状态变量
- postgresql - PostgreSQL:计算列中的出现次数
- php - 使用 php 从数据库更新上传的文件