返回文件夹ID,但不创建文件夹 [英] Returning folder ID but not creating folder

查看:79
本文介绍了返回文件夹ID,但不创建文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码

define('DRIVE_SCOPE', 'https://www.googleapis.com/auth/drive');
define('SERVICE_ACCOUNT_EMAIL', 'xxx.iam.gserviceaccount.com');
define('SERVICE_ACCOUNT_PKCS12_FILE_PATH', 'xxx');

function buildService($userEmail) {
    $key = file_get_contents(SERVICE_ACCOUNT_PKCS12_FILE_PATH);
    $auth = new Google_Auth_AssertionCredentials(
    SERVICE_ACCOUNT_EMAIL,
    array(DRIVE_SCOPE),
    $key);
    //$auth->sub = $userEmail;
    $client = new Google_Client();
    $client->setAssertionCredentials($auth);
    return new Google_Service_Drive($client);
}

$service = buildService('xxx.apps.googleusercontent.com');

$fileMetadata = new Google_Service_Drive_DriveFile(array(
    'name' => 'Invoices',
    'mimeType' => 'application/vnd.google-apps.folder'
));

$file = $service->files->create($fileMetadata, array(
    'fields' => 'id'
));

printf("Folder ID: %s\n", $file->id);

我一直试图通过PHP在我的驱动器帐户上创建一个文件夹.我相信所有配置都正确,并且得到了结果

I have been trying to create a folder on my drive account through PHP. I believe everything is configured correctly and I am getting a result

文件夹ID:0B2nllBpB_k0NQWFNUjlSc0NUdE0

Folder ID: 0B2nllBpB_k0NQWFNUjlSc0NUdE0

但是没有在我的驱动器帐户上创建该文件夹.创建文件也是如此.它不断返回ID,但实际上未创建任何内容.我到底在做错什么?

But the folder is not being created on my drive account. The same goes with creating files as well. It keeps returning an ID but nothing is actually created. What exactly am I doing wrong?

推荐答案

您当前的代码正在作为服务帐户进行身份验证并正在创建文件.这样,生成的文件将只能由服务帐户访问,而不能由您的Google用户访问.您可以通过执行文件列表而不是代码中的创建来确认这一点,您将取回先前创建的文件.

Your current code is authenticating as the service account and creating a file. The resultant file would then only be accessible to the service account, not your Google user. You can confirm this by doing a list of files instead of a create in your code, you'll get back the files you've previously created.

您的代码已将sub =注释掉.取消注释该行,并确保您已按照域范围内的授权说明进行操作,因此您的代码可以进行身份​​验证并充当您的Google用户.

Your code has sub= commented out. Uncommented that line and make sure you've followed the domain-wide delegation instructions so that your code can authenticate and act as your Google user.

这篇关于返回文件夹ID,但不创建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆