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.