I want to use Google Drive to store my application files without user authentication. I have already seen Use Google Drive API with Laravel but the answers do not match with my needs.

我想使用Google Drive存储我的应用程序文件而无需用户身份验证。我已经看过在Laravel中使用Google Drive API,但答案与我的需求不符。

I have also tried an ajax post request when form submitted.

我还在表单提交时尝试了ajax post请求。

I want to know how do I get this request that I have found in Google Drive documentation: https://developers.google.com/drive/v3/web/manage-uploads .

我想知道如何获得我在Google云端硬盘文档中找到的请求:https://developers.google.com/drive/v3/web/manage-uploads。

  1. POST /upload/drive/v3/files?uploadType=media HTTP/1.1
  2. POST / upload / drive / v3 / files?uploadType = media HTTP / 1.1

  3. Host: www.googleapis.com
  4. Content-Type: image/jpeg
  5. Content-Length: number_of_bytes_in_file
  6. Authorization: Bearer your_auth_token
  7. 授权:Bearer your_auth_token

JPEG data

2 个解决方案

#1


0

As have been mentioned in the comment and from Daimto's answer, Google Drive (and other SaaS tools) requires authentication, for security purposes.

正如评论和Daimto的回答中所提到的,出于安全考虑,Google Drive(和其他SaaS工具)需要身份验证。

Try the PHP Quickstart in Drive API v3. The basic structure is already there.

在Drive API v3中尝试PHP快速入门。基本结构已经存在。

For an actual example of uploading a file using the API, this SO thread might help:

有关使用API​​上传文件的实际示例,此SO线程可能有所帮助:

require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';

$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('<YOUR_CLIENT_ID>');
$client->setClientSecret('<YOUR_CLIENT_SECRET>');
$client->setRedirectUri('<YOUR_REGISTERED_REDIRECT_URI>');
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));

session_start();

if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
} else
$client->setAccessToken($_SESSION['access_token']);

$service = new Google_Service_Drive($client);

//Insert a file
$file = new Google_Service_Drive_DriveFile();
$file->setTitle(uniqid().'.jpg');
$file->setDescription('A test document');
$file->setMimeType('image/jpeg');

$data = file_get_contents('a.jpg');

$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => 'image/jpeg',
'uploadType' => 'multipart'
));

print_r($createdFile);

} else {
$authUrl = $client->createAuthUrl();
header('Location: ' . $authUrl);
exit();
}

There's also a google/google-api-php-client in Github. The guide might offer you insight as well.

在Github中还有一个google / google-api-php-client。该指南也可能为您提供见解。

更多相关文章

  1. 来自php表单的样式电子邮件
  2. 请问如何用php实现表单提交后以邮件的形式把表单内容发到邮箱中
  3. 如何在jQuery Mobile页面中提交表单?
  4. Symfony2 -从数据库中提取数据并以表单形式显示
  5. laravel 框架自带表单验证
  6. AJAX学习之提交表单
  7. 在提交注册表单时使用jQuery显示错误
  8. 如果改变输入值,jQuery提交表单
  9. 如何在表单操作中执行PHP函数?

随机推荐

  1. C++在构造函数中使用new时,需要注意这些事
  2. 在什么语言中字符串以\0标志字符串的结
  3. 声明动态数组的语句怎么写
  4. 详解C++虚成员函数和动态联编
  5. c语言中文本输出的函数名称是什么?
  6. c语言函数声明格式是什么?
  7. c语言绝对值怎么表示
  8. c语言中逻辑运算符优先级是什么?
  9. 对比分析C#与Java的区别
  10. c语言中double是什么意思