简体   繁体   English

没有 google api 库的 Google 驱动器上传

[英]Google drive upload with out the google api library

Can we upload files by creating a request from PHP with out using the google api library ?我们可以通过从 PHP 创建请求而不使用 google api 库来上传文件吗?

https://developers.google.com/drive/api/guides/manage-uploads#http_1 https://developers.google.com/drive/api/guides/manage-uploads#http_1

Yes you can, it's just HTTP requests.是的,你可以,这只是 HTTP 请求。 But it's likely to be simpler to use the library.但是使用该库可能更简单。

The google drive api is a simple rest api. google drive api是一个简单的rest api。 As long as you can send a http post you will be able to upload the file.只要您可以发送http post,您就可以上传文件。

POST https://www.googleapis.com/drive/v3/files?key=[YOUR_API_KEY] HTTP/1.1

Authorization: Bearer [YOUR_ACCESS_TOKEN]
Accept: application/json
Content-Type: application/json

{body}

There is some documentation on how to format the file for upload manage-uploads#http Pay close attention to the Content-Type for the file meta data, As well as how to format the metadata for the file itself.有一些关于如何格式化文件以进行上传的文档manage-uploads#http请密切注意文件元数据的 Content-Type 以及如何格式化文件本身的元数据。 You may want to rip a bit of code out of the client library to start with MediaFileUpload.php您可能想从客户端库中提取一些代码以从MediaFileUpload.php开始

The tricky part will be your authorization.棘手的部分将是您的授权。 You need to be sure to send a valid access token with every request.您需要确保在每个请求中都发送一个有效的访问令牌。 Oauth2 authorization is in the form of three requests. Oauth2 授权是三个请求的形式。

  1. Request consent -> results in authorization code请求同意 -> 产生授权码
  2. Exchange authorization code -> results in access token and refresh token交换授权码 -> 产生访问令牌和刷新令牌
  3. Exchange refresh token for new access token.将刷新令牌换成新的访问令牌。

More information can be found here web-server#httprest更多信息可以在这里找到web-server#httprest

I strongly suggest that you go with the PHP client library.我强烈建议您使用 PHP 客户端库。

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

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