Download a file from Google Drive using Google Picker

I am trying to create a web page where a user can select an image from Google Drive using Google Picker and upload the selected files to his server using a PHP script.

I managed to configure Picker and I get fileID files, but when I transfer these identifiers to my server and try the GET method, I get an authentication error.

I spent 2 days working on it and researching, but the more I read the official Google documentation, the more confused I am.

Can someone tell me or link me an example of how to implement this? Is it possible to somehow transfer the oAuthv2 token from GooglePicker to my PHP server and then use this token with a GET request?

Thank you in advance!

edit:

here is the error i get when i try to get https://www.googleapis.com/drive/v2/files/SOME_FILE_ID

{ "error": { "errors": [ { "domain": "usageLimits", "reason": "dailyLimitExceededUnreg", "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.", "extendedHelp": "https://code.google.com/apis/console" } ], "code": 403, "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup." } 
+4
source share
3 answers

Before calling GET, you must set an authorization header containing the updated access token.

If you want to do this manually, follow these steps: -

  • Request permission using the id / client-id areas of the application. This will return the authorization code to you.
  • Use authorization code to request update token and access token
  • Save update token for future use
  • Set the access token in the HTTP authorization header (something like authorization: Bearer ya29.AHES6ZR3HQa9trJM_IQcgNlM0SI4FvLQFiQfcAZCWLobfpjqtGlT6A)
  • Do get

You can see the whole process in action by clicking on https://developers.google.com/oauthplayground/

Alternatively, if your client application already has an access token, you can send it to your server along with the file identifier, and your server can simply set it directly in the authorization header.

Instead, there are PHP libraries, for example. go to https://developers.google.com/drive/v2/reference/files/insert and scroll down to view PHP samples. It is your choice, whether you create URLs manually or use libraries. The big drawback for libraries is that if something goes wrong, you really need to understand and follow http anyway to find out what happens, so you can learn and love them from day one.

The message "Daily Limit for Unauthenticated Use Exceeded" seems to confuse the first timers (including me). This is an “unauthenticated benefit” that is an important part, that is, you did not specify an authorization header. Google APIs have a daily quota for unauthorized use (e.g. URL shortening). In the case of Disk, this quota is ZERO, therefore, an error.

+4
source

@pinoyyid said all that is, and inspired him, here is the actual solution I came up with:

If you want to upload a file, you need two variables - oAuthToken and fileId

oAuthToken you receive from the client side JS when the user authenticates. If you use the example from google docs ( https://developers.google.com/picker/docs/ ), the function looks like this:

 function handleAuthResult(authResult) { if (authResult && !authResult.error) { oauthToken = authResult.access_token; oauthToken; // <-- THIS IS THE Bearer token createPicker(); } } 

fileId you get when the user selects a file. Again, a modified example from google docs:

 function pickerCallback(data) { if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) { var doc = data[google.picker.Response.DOCUMENTS][0]; alert('You picked fileId: ' + doc[google.picker.Document.ID]); } } 

You will probably pass this data as a form request or through ajax. A simple cURL call from the backend to upload a file:

 $oAuthToken = 'ya29.XXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXX-XXXXXXX-XXXXXX-X-XXXXXXXXXXXX-XXXX'; $fileId = '0B4zzcXXXXXXXXXXXXXXXXXXXXXX'; $getUrl = 'https://www.googleapis.com/drive/v2/files/' . $fileId . '?alt=media'; $authHeader = 'Authorization: Bearer ' . $oAuthToken ; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $getUrl); curl_setopt($ch, CURLOPT_HTTPHEADER, [ $authHeader , ]); $data = curl_exec($ch); $error = curl_error($ch); curl_close($ch); file_put_contents("destination-file.jpg", $data); 

File upload docs: https://developers.google.com/drive/web/manage-downloads

+1
source

You save the update token in the database. When checking the API call, whether the current token is saved. If not, the new access token is retrieved using the update token.

0
source

Source: https://habr.com/ru/post/1500020/


All Articles