Skip to content
Advertisement

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 ?

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

Advertisement

Answer

The google drive api is a simple rest api. As long as you can send a http post you will be able to upload the file.

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. You may want to rip a bit of code out of the client library to start with 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.

  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

I strongly suggest that you go with the PHP client library.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement