将文件从App Engine上传到Google云端硬盘

时间:2015-04-20 18:59:58

标签: php google-app-engine google-drive-api

最后,我想通过App Engine php脚本将文件上传到我的Google云端硬盘。

但是,遵循https://developers.google.com/drive/web/quickstart/quickstart-php之类的教程 强迫个人登录。我假设在这种情况下,我将不得不使用服务帐户凭据。

从这里,我上传了以下代码以及所需的API客户端库文件

<?php
require_once 'autoload.php';
//require_once 'Auth/AssertionCredentials.php';
require_once 'Client.php';
require_once 'Service/Drive.php';

//
$client = new Google_Client();

$client->setApplicationName("blah-blah-00000");
$client->setClientID("000000000000-00000000000000000000000000000000.apps.googleusercontent.com");

$service = new Google_Service_Drive($client);

// This file location should point to the private key file.
$key = file_get_contents("BlahBlah-000000000000.p12");

$cred = new Google_Auth_AssertionCredentials(
  "000000000000-00000000000000000000000000000000@developer.gserviceaccount.com",
  // Replace this with the scopes you are requesting.
  array('https://www.googleapis.com/auth/drive',
        'https://www.googleapis.com/auth/drive.file'),
  $key
);
$client->setAssertionCredentials($cred);


//Insert a file
$file = new Google_Service_Drive_DriveFile();
$file->setTitle('HelloWorld');
$file->setDescription('A test document');
$file->setMimeType('text/plain');

$data = "Hello World!";

$createdFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => 'text/plain',
      'uploadType' => 'media'
    ));

//print_r($createdFile);

?>

访问相关的appspot.com后,该页面为空白。此时,我查看与该帐户关联的Google云端硬盘;但是没有找到新文件。

我使用服务帐户是否正确?如果这是正确的,我的下一个问题是我是否正确设置了凭据?我是否必须冒充我的用户帐户/ App Engine应用程序设置的帐户?

2 个答案:

答案 0 :(得分:2)

我使用服务帐户是否正确?

可能不是。我看到很多人在使用服务帐户时应该使用的是离线访问标准帐户。  &#34;此时,我查看与该帐户关联的Google云端硬盘;但是没有找到新文件&#34; 。具体而言,人们没有意识到服务帐户与其标准帐户相关联,因此在查看其标准帐户的云端硬盘时,服务帐户中的文件就不存在。他们位于没有用户界面的服务帐户中。

如果这是正确的,我的下一个问题是我是否正确设置了凭据?

不是。你说&#34;以下教程如...强制个人登录。&#34;没有任何教程可以显示您的用例,但这并不意味着它无法完成。只是没有人写过如何做到这一点的教程。

我是否必须冒充我的用户帐户/ App Engine应用程序设置的帐户?

你需要: -

  1. 对您的帐户进行一次性授权,请求离线访问。这将为您提供需要保存的刷新令牌。
  2. 然后,当您想要访问Drive时,使用Refresh Token请求访问令牌。
  3. 好消息是第1步并不要求您编写任何代码。见How do I authorise an app (web or installed) without user intervention? (canonical ?)

答案 1 :(得分:0)

在关注pinoyyid的LINK并使用离线OAuth2刷新令牌之后,我将代码更新为以下内容

<?php
require_once 'autoload.php';
require_once 'Client.php';
require_once 'Service/Drive.php';

//
$client = new Google_Client();
// Replace this with your application name.
$client->setApplicationName("00000-00000-00000");
$client->setClientID("000000000000-00000000000000000000000000000000.apps.googleusercontent.com");
$client->setClientSecret("XXXXXXXXXXXXXXXXXXXX");
$client->refreshToken("1/KXXXXXXXXXXXXXXXXXXXXXXXXX");

// Replace this with the service you are using.
$service = new Google_Service_Drive($client);

$_SESSION['service_token'] = $client->getAccessToken();

//Insert a file
$file = new Google_Service_Drive_DriveFile();
$file->setTitle('HelloWorld');
$file->setDescription('A test document');
$file->setMimeType('text/plain');

$data = "Hello World!";

$createdFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => 'text/plain',
      'uploadType' => 'media'
    ));

?>
相关问题