doutiaoku4495 2014-08-06 17:50
浏览 63

Google云端硬盘上传PHP程序化登录

I am developing a web API for a mobile app which needs to store files. Currently, the mobile clients send a request to the API with the file's data, and the file is stored directly on the server. However, I need to save space on the server and this is just a temporary solution.

What I would like to do is have the mobile client upload the file to the API, which then uploads that file to Google Drive. I have taken a look at the Google Drive API and I got it to upload a file from my home computer successfully, but in order to do so, I needed to sign in to my google account to authorize the upload. Here is the code:

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

$client = new Google_Client();
// Get your credentials from the console
$client->setClientId(' my client id ');
$client->setClientSecret(' my client secret ');
$client->setRedirectUri('https://www.hwassassin.net16.net/oauth2callback');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));

$service = new Google_DriveService($client);

$authUrl = $client->createAuthUrl();

//Request authorization
print "Please visit:
$authUrl

";
print "Please enter the auth code:
";

$authCode = trim(fgets(STDIN));

// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);

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

$data = file_get_contents('document.txt');

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

print_r($createdFile);
?>

Since I need to upload the file from the backend, I would like to know if I can somehow programmatically log in to my Google Drive account from the server, without requiring the Client to take any action.

If this is not even possible, I would appreciate any alternative cloud storage API's that don't require the client to sign in to an account.

Any help would be greatly appreciated!

  • 写回答

1条回答 默认 最新

  • dongluni0568 2014-09-04 11:37
    关注

    Solution is using application owned accounts using service accounts or regular accounts. Please follow the directions at https://developers.google.com/drive/web/service-accounts.

    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛