2

I have a problem while uploading a file to Google Drive, I can't upload file. Anyone can help, please answer.

Hereunder is the error I got while uploading file:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}

I have registered the application with the drive SDK and I am using the drive SDK client ID. Also I am using the updated access token everytime from the long-lived refresh token saved in DB.

The code is here:

$fname = basename($updfile); // where $updfile is the file name with path
$turl = "https://www.googleapis.com/drive/v1/files";
$jsondata = '
{
    "title": "'.$fname.'",
    "mimeType":"application/zip",
    "description":"The mailchimp data export to google drive."
}';

$headers = 'Host: www.googleapis.com'."\r\n";
$headers .= 'Authorization: Bearer '.$access_token."\r\n";  // $access_token is token generating everytime from refresh token
$headers .= 'Content-Type: application/json'."\r\n";
$headers .='Content-Length: '.strlen($jsondata)."\r\n";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $turl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsondata);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($headers));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);

Please revert/answer, if anyone have the solution or suggestion regarding this. Thanks in Advance.

3 Answers3

0

I'd recommend using the PHP client library and checking the documentation for a PHP snippet showing how to upload a file to Google Drive:

https://developers.google.com/drive/v2/reference/files/insert

If for any reasons you want to keep using php_curl and manually construct the HTTP request, you should try sending the same request with the OAuth 2.0 Playground and compare it with yours.

Claudio Cherubino
  • 14,896
  • 1
  • 35
  • 42
  • the php client library has no documentation whatsoever for doing things like this except for your "simple" upload example. Not one mention of how a person might use the "recommended" resumable upload feature. I hope google has a look at how jQuery.com and php.net provides documentation on how to implement their libraries. It is why they "just work"... google just has a bunch of sample pages (which don't even work most of the time) and an empty wiki. Not to mention that you are asking us to download straight from the trunk which is out of step with the "release" download. Very unprofessional!! – pathfinder Nov 16 '12 at 21:10
0

Why not try to use your access token in a rest client to see if the problem comes from your PHP code or your access token.

Postman Rest Client for Chrome is very simple to use.

Yacine Rezgui
  • 1,771
  • 1
  • 18
  • 15
0

I think your url is incorrect:

$turl = "https://www.googleapis.com/drive/v1/files";

It should be:

$turl = "https://www.googleapis.com/upload/drive/v1/files";

Here are the docs (as thin as they are) on using the v2 upload features. It may or may not be more useful than using v1 :)

https://developers.google.com/drive/manage-uploads

pathfinder
  • 1,606
  • 18
  • 22