duankang8114 2016-03-06 04:27
浏览 21
已采纳

返回文件夹ID但不创建文件夹

Using the code below

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
", $file->id);

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

Folder ID: 0B2nllBpB_k0NQWFNUjlSc0NUdE0

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?

  • 写回答

1条回答 默认 最新

  • dongpo5239 2016-03-06 12:55
    关注

    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.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序