PHP: Google Drive file upload failing with 401 authorization error

I’m having trouble uploading files to Google Drive using PHP. The weird thing is I can get my files just fine, but when I try to upload, it’s not working.

Here’s what I’m seeing:

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

I’ve set up my app with the Drive SDK and I’m using the right client ID. The token works for getting files, so I’m pretty sure it’s valid.

Here’s my code:

function upload_document($auth_token) {
  $content = file_get_contents("document.txt");
  $content_length = strlen($content);
  $upload_url = "https://www.googleapis.com/upload/drive/v2/files";
  
  $headers = [
    "Host: www.googleapis.com",
    "Authorization: Bearer $auth_token"
  ];
  
  $curl = curl_init();
  $file_data = ["file" => "@document.txt"];
  
  curl_setopt($curl, CURLOPT_URL, $upload_url);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $file_data);
  
  $response = curl_exec($curl);
  echo $response;
}

Any ideas what could be causing this? I’m stumped!

I’ve encountered similar issues before, and it can be quite frustrating. One thing that often gets overlooked is the difference in scopes required for reading vs. writing to Google Drive. While your token might work for fetching files, it may not have the necessary permissions for uploads.

Try modifying your authorization request to include the ‘https://www.googleapis.com/auth/drive.file’ scope explicitly. This grants access to files created or opened by the app. Also, ensure you’re using the v3 API endpoint (https://www.googleapis.com/upload/drive/v3/files) as it’s more current and might resolve some compatibility issues.

Lastly, I’d recommend using Google’s official PHP client library instead of raw cURL requests. It handles a lot of the low-level details and token management for you, which can save you from headaches like this in the future.

I’ve dealt with this issue before, and it can be tricky. Have you checked if your OAuth 2.0 credentials are set up correctly in the Google Cloud Console? Sometimes, the problem lies in the configuration rather than the code itself.

Also, I noticed you’re using the v2 API endpoint. Google recommends using v3 for new projects. Try updating your upload_url to ‘https://www.googleapis.com/upload/drive/v3/files’ and see if that helps.

Another thing to consider is the Content-Type header. For file uploads, you should specify the MIME type of the file you’re uploading. Add this to your headers array:

“Content-Type: text/plain”

(Assuming your document.txt is a plain text file)

If none of these work, try implementing exponential backoff for your requests. Sometimes, temporary server issues can cause 401 errors, and retrying with increasing delays can resolve the problem.

hey man, i had this exact problem last week. turned out my auth token was expired. google tokens only last like an hour or something. try refreshing ur token before uploading and see if that fixes it. also, double check ur scopes to make sure u have write access