0

I want to write a function that I can upload, download, and delete files from my google drive by using curl in php. Therefore, I'll have to get the access token before uploading any files. The trouble I'm facing is that I got my refresh token, and I did enabled my gdrive api, but I don't know what should by CURLOPT_URL, CURLOPT_POSTFIELDS, and CURL_HTTPHEADER be. The code below showed what I'm trying to do:

<?php 
  $post = array("grant_type" => "refresh_token",
                "client_id" => "MYCLIENT",
                "client_secret" => "MYCLIENTSECRET",
                "refresh_token" => "MYREFRESHTOKEN",
                "Content-length" => "163");

  $header = array("Content-type" => "application/x-www-form-urlencoded",
                  "Content-length" => "163");

  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, "www.googleapis.com/oauth2/v4/token");
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  curl_setopt($curl, CURLOPT_POST, $post);

  $output = curl_exec($curl);
  curl_close($curl);

  print $output;

?>

It keeps telling me "411. That’s an error. POST requests require a Content-length header. That’s all we know." However, I already put content length in both post field and header so I assume the problem might be something else.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
Daniel
  • 27
  • 7

2 Answers2

1

This post might help How do I authorise an app (web or installed) without user intervention? (canonical ?) , specifically the reference to https://developers.google.com/identity/protocols/OAuth2WebServer#offline

Community
  • 1
  • 1
pinoyyid
  • 21,499
  • 14
  • 64
  • 115
0

Yes. Error 411 means that you must provide the Content-Length HTTP header. This error has no response body. Try to remove the Content-Length part from your header array.

//remove following
$header[] = 'Content-Length: ' . $length;

From this SO thread, cURL adds it automatically. So you don't need to send that.

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59