繁体   English   中英

Google云端硬盘服务帐户上传的位置信息

[英]Location of Google Drive Service Account Uploads

我正在尝试使用服务帐户将文件上传到我的Google云端硬盘。 当我部署此代码时,我不希望用户授权,我希望他们上传到我的帐户。 我通过PHP使用它,下面是我到目前为止的地方。

此代码基于官方文档提供的示例。 当我运行php脚本时,我没有错误,我print_r结果变量。 它有多个网址,当我使用我创建的相同帐户访问它时,它说我需要请求权限。 当我查看我的Google Developers控制台时,它说它收到了请求并成功运行了它。

但是,该文件不会显示在我的Google云端硬盘中。 我不确定在服务帐户上查看这些文件的位置,以及如何将此文件存入我的Google云端硬盘。 以下是我的代码。

我在某个地方错过了许可,或者我应该以某种方式将服务帐户连接到普通帐户?

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驱动器帐户而不是您的帐户。

我建议您使用相同的脚本并执行文件列表以查看该文件是否已上载到服务帐户驱动器帐户。

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

“我正在尝试使用服务帐户将文件上传到我的Google云端硬盘。”

正如DaImTo所说,服务帐户与您自己的云端硬盘帐户不同。 线索在“帐户”一词中,即。 这是一个单独的帐户。

如果您要上传到自己的帐户,只需获取该帐户的刷新令牌,然后在每次要上传时使用它来生成访问令牌。 此处记录了所需的步骤如何在没有用户干预的情况下授权应用程序(Web或已安装)? (规范?)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM