3

I am working with YouTube APIs for my college project, and I keep getting an error. Here I send them to the authorisation page to log in, when they allow access it sends the $_GET['code'] string back. Then I send this along with some other data and it should send back a JSON object. Instead I am just getting

Warning: file_get_contents(https://accounts.google.com/o/oauth2/token) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in http://www.example.net/callback.php on line 27

I have replaced my domain with example.net just for security

 urlencode($_GET['code']),
                'client_id' => urlencode('111522767640.apps.googleusercontent.com '),
                'client_secret' => urlencode('secret'),
                'redirect_uri' => urlencode('http://example.net/callback.php'),
                'grant_type' => urlencode('authorization_code')
            )
        );

        $params = 
        array('http' =>
            array(
                'method'  => 'POST /o/oauth2/token HTTP/1.1',
                'header'  => 'Host: accounts.google.com\r\n'.                           
                            'Content-Type: application/x-www-form-urlencoded',
                'content' => $postdata
            )
        );

        $context = stream_context_create($params);
        $result = file_get_contents('https://accounts.google.com/o/oauth2/token', false,$context);
        var_dump($_SESSION);
        var_dump($result);
    }
    else //If code isnt set, user must have come here erroniously or has denied access to this program
    {
        //header( 'Location: www.example.net/error.php' ) ;
    }

?>
Jonathan Deakin
  • 100
  • 2
  • 6

3 Answers3

0

if you are using oauth2, goto libraries/oauth2/provider.php and uncomment the code line 182 shows

            $ci = get_instance();
            $ci->load->spark('curl/1.2.1');
            $ci->curl
                ->create($url)
                ->post($params, array('failonerror' => false));

            $response = $ci->curl->execute();
Raveendra
  • 141
  • 2
  • 6
0

Here's how to do the Google oAuth properly:

$config = (object) array(
  'CLIENT_ID' => 'AAAAA',
  'CLIENT_SECRET' => 'BBBBB',
  'REFRESH_TOKEN' => 'CCCCC',
);
$sJSON = file_get_contents('https://www.googleapis.com/oauth2/v4/token',FALSE,
  stream_context_create(array('http'=>array(
      'ignore_errors' => TRUE, // see errors in response instead of empty on error
      'method' => 'POST',
      'header' => array(
        'Content-Type: application/x-www-form-urlencoded'
      ),
      'content' => http_build_query(array(
        'grant_type' => 'refresh_token',
        'client_id' => $config->CLIENT_ID,
        'client_secret' => $config->CLIENT_SECRET,
        'refresh_token' => $config->REFRESH_TOKEN
      ))
  )))
);

You can then use json_decode() to parse $sJSON into an object and then retrieve the access_token property.

For those who are wondering how to get the CLIENT_ID, CLIENT_SECRET, and REFRESH_TOKEN, watch this video. It's not easy. In my case, I needed to do this for Google Adwords API. So, I had to get my Developer Token and Login Customer ID from https://ads.google.com/home/tools/manager-accounts. Then, I had to go to https://console.developers.google.com/apis/credentials with this guide to generate a Client ID and Client Secret. Then, I followed this guide to learn how to get my Refresh Token.

Volomike
  • 23,743
  • 21
  • 113
  • 209
0

file_get_contents is going to make a GET request to the url specified, but oauth2/token needs a POST request.

See reference Google OAuth2, PHP HTTP.

dldnh
  • 8,923
  • 3
  • 40
  • 52
  • Ahah, thank you, I was mixing and matching examples off the internet so I must have ended up with a GET – Jonathan Deakin Mar 24 '12 at 20:23
  • 3
    What about this question http://stackoverflow.com/a/2445332 ? Seems it is still possible to send POST with file_get_contents() – vellotis Jul 04 '12 at 21:53