Google云端硬盘服务帐户上传的位置 [英] Location of Google Drive Service Account Uploads

查看:118
本文介绍了Google云端硬盘服务帐户上传的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用服务帐户将文件上传到我的Google云端硬盘.部署此代码时,我不希望用户给予授权,而是希望他们上载到我的帐户.我正在通过PHP使用它,到目前为止,这里是到目前为止.

I'm trying to upload a file to my Google Drive using a service account. When I deploy this code, I don't want the user to give authorization, I want them to upload to my account. I'm using this via PHP, and below is where I am so far.

此代码基于官方文档给出的示例.当我运行php脚本时,没有任何错误,并且我将结果变量print_r了.它具有多个URL,当我使用创建该文件的相同帐户访问该URL时,它说我需要请求权限.当我在Google Developers控制台中查看时,它表示已收到请求并成功运行了该请求.

This code is based on the examples given by the official documentation. When I run the php script, I get no errors, and I print_r the result variable. It has multiple URLs, when I go to it with the same account I created all this with, it says I need to request permission. When I look in my Google Developers console, it says it got the request and successfully ran it.

但是,该文件未显示在我的Google云端硬盘中的任何位置.我不确定如何在服务帐户中看到这些文件,以及如何将该文件放入我的Google云端硬盘.下面是我的代码.

However, the file doesn't show anywhere in my Google Drive. I'm not really sure where to see these files on the service account, and how to get this file into my Google Drive. Below is my code.

我是否在某处缺少许可,还是应该以某种方式将服务帐户连接到普通帐户?

Am I missing a permission somewhere, or am I supposed to somehow connect the service account to a normal account?

DEFINE("TESTFILE", 'testfile-small.txt');
    if (!file_exists(TESTFILE)) {
      $fh = fopen(TESTFILE, 'w');
      fseek($fh, 1024 * 1024);
      fwrite($fh, "!", 1);
      fclose($fh);
    }

// The setup

// Authentication Credentials
$client_id = '<my client id>'; //Client ID
$service_account_name = '<my client email'; //Email Address
$key_file_location = '<path to key>'; //key.p12

// Create the client
$client = new Google_Client();
$client->setApplicationName("IEEE_File_Upload");
$service = new Google_Service_Drive($client);

// Check for token
if (isset($_SESSION['service_token'])) {
  $client->setAccessToken($_SESSION['service_token']);
}

// Get the key
$key = file_get_contents($key_file_location);

// Create the credentials
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://www.googleapis.com/auth/drive'),
    $key
);

// Set the credentials
$client->setAssertionCredentials($cred);

// Something with tokens
if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();

// Actual file stuff

  // Now lets try and send the metadata as well using multipart!
  $file = new Google_Service_Drive_DriveFile();
  $file->setTitle("Hello World!");
  $result = $service->files->insert(
      $file,
      array(
        'data' => file_get_contents(TESTFILE),
        'mimeType' => 'application/octet-stream',
        'uploadType' => 'multipart'
      )
  );

echo "<h3>Results Of Call:</h3>";
foreach ($result as $item) {
  echo $item['volumeInfo']['title'], "<br /> \n";

推荐答案

服务帐户是不是您,服务帐户是其自身的sudo用户实体类型.服务帐户具有其自己的驱动器帐户,其自己的Google日历..当您将文件上传到服务帐户时,文件将上传到其Google驱动器帐户,而不是您的帐户.

A service account is NOT YOU, a service account is its own sudo user entity type of thing. A service account has its own drive account, its own Google calendar .. when you preform an upload to a service account files are uploaded to its Google drive account not yours.

我建议您使用相同的脚本并做一个文件列表,以查看文件是否已上传到服务帐户驱动器帐户.

I suggest you use the same script and do a file list to see if the file was uploaded to the service accounts drive account.

服务帐户没有Web界面,因此您无法登录并通过Web对其进行检查,这都必须通过您的服务帐户代码来完成.

There is no web interface for a service account so you cant log in and check it via the web it will all have to be done though your service account code.

这篇关于Google云端硬盘服务帐户上传的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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