0
$ret = file_get_contents('https://oauth2.googleapis.com/token', false, stream_context_create([
    'http' => [
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'protocol_version' => 1.1,
        'content' => http_build_query([
            'client_id' => $client_id,
            'client_secret' => $client_secret,
            'refresh_token' => $refresh_token,
            'grant_type' => $refresh_token
        ])
    ]
]));

After running the script, I see a 400 Bad Request error. What did I do wrong?

jajaja
  • 1
  • `\r\n` at the end of the header value definitively looks out of place. If you were passing _multiple_ headers in string form, then there should be a CR LF between each of them, but with just one, I am pretty sure that’s wrong. – 04FS Oct 28 '19 at 12:36
  • Yes, he still is not needed there – jajaja Oct 28 '19 at 13:08

2 Answers2

0

From: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400

The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

That means that most likely, something is wrong with the parameters you are sending in the request. That may not be it, but try removing the \r\n in your header parameter, they seem odd to me.

$ret = file_get_contents('https://oauth2.googleapis.com/token', false, stream_context_create([
    'http' => [
        'header'  => "Content-type: application/x-www-form-urlencoded",
        'method'  => 'POST',
        'protocol_version' => 1.1,
        'content' => http_build_query([
            'client_id' => $client_id,
            'client_secret' => $client_secret,
            'refresh_token' => $refresh_token,
            'grant_type' => $refresh_token
        ])
    ]
]));
Lucas Meine
  • 1,524
  • 4
  • 23
  • 32
  • It turned out, but the following problem arose. After execution, I see a white screen and var_dump returns nothing – jajaja Oct 28 '19 at 13:07
  • @jajaja var_dump does not ever return “nothing”. If you mean you get no output at all from a script that should show at least some, then at this point this would probably a duplicate of https://stackoverflow.com/questions/1475297/phps-white-screen-of-death – 04FS Oct 28 '19 at 13:11
  • If `file_get_contents` just returns `false`, then that’s usually due to the request getting a response with a non-success status code, in that case file_get_contents will discard any response body, unless you explicitly instruct it to behave otherwise (parameter `ignore_errors` set to true in the HTTP context options.) – 04FS Oct 28 '19 at 13:14
  • I accidentally forgot to remove the exit() line when testing. Now I checked and it gives me an error: HTTP/1.1 400 Bad Request – jajaja Oct 28 '19 at 13:24
  • Maybe he sends the headers not like this? https://developers.google.com/youtube/v3/guides/auth/server-side-web-apps#offline What is the best way to check what it sends to the server? – jajaja Oct 28 '19 at 13:25
0

The solution is very simple:

'content' => http_build_query([
      client_id' => $client_id,
     'client_secret' => $client_secret,
     'refresh_token' => $refresh_token,
     'grant_type' => 'refresh_token'
 ])
jajaja
  • 1