Question

I found the code below on the net (here) and tried implementing it on Google App Engine.

It works perfectly when i ran it on localhost, but when i deployed it to App Enginge i get nothing in $encodedData.

$authContext = stream_context_create(array(
    'http' => array(
        'method'  => 'POST',
        'header'  => "Authorization: Basic " . base64_encode(($consumerKey).':'.($consumerSecret)) . "\r\n".
                "Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n".
                "Content-Length: 29\r\n".
                "\r\n".
                "grant_type=client_credentials",
    ),
));
$authResponse = file_get_contents("https://api.twitter.com/oauth2/token", false, $authContext);
$decodedAuth = json_decode($authResponse, true);
$bearerToken = $decodedAuth["access_token"];

$context = stream_context_create(array(
    'http' => array(
        'method'  => 'GET',
        'header'  => "Authorization: Bearer " . $bearerToken . "\r\n".
                     "\r\n".
                     "grant_type=client_credentials",
    ),
));

$encodedData = file_get_contents('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=samkiesupdates&count='."100", false, $context);

Anyone who knows what i need to alter in the code to have it work on the Google App Engine server? Maybe it says in the replies to the forum post i linked above, but sadly i don't speak german so i can't tell.

Was it helpful?

Solution

You should be putting the post body in the 'content' field of the http context options, and no need to specify the content length.

$authContext = stream_context_create(array(
    'http' => array(
        'method'  => 'POST',
        'header'  => "Authorization: Basic ".base64_encode(($consumerKey).':'.($consumerSecret))."\r\n".
                "Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n",
        'content' => 'grant_type=client_credentials',
    ),
));

Do that same for the other http request you make.

OTHER TIPS

Just to clear this stuff up, and to make this whole question / answer more visible. Here is the working code example:

  <?php

    $consumerKey    = <YOUR CONSUMER KEY>;

    $consumerSecret = <YOUR CONSUMER SECRET>;

    $authContext = stream_context_create(array(

      'http' => array(

        'method'  => 'POST',

        'header'  => "Authorization: Basic " . base64_encode(($consumerKey).':'.($consumerSecret)) . "\r\n".

          "Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n",

        'content' => "grant_type=client_credentials"

        ),

    ));

    $authResponse = file_get_contents("https://api.twitter.com/oauth2/token", false, $authContext);

    $decodedAuth = json_decode($authResponse, true);

    $bearerToken = $decodedAuth["access_token"];

    $context = stream_context_create(array(

      'http' => array(

          'method'  => 'GET',

          'header'  => "Authorization: Bearer " . $bearerToken . "\r\n",

          'content' => "grant_type=client_credentials"

        ),

    ));

    $encodedData = file_get_contents('https://api.twitter.com/1.1/statuses/show.json?id=<YOUR TWEET ID>', false, $context);


    $result = json_decode($encodedData);

Thanks for the help,

Nick

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top