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
CodePudding user response:
Yes you can, it's just HTTP requests. But it's likely to be simpler to use the library.
CodePudding user response:
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.
- Request consent -> results in authorization code
- Exchange authorization code -> results in access token and refresh token
- 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.