Question

I have to implement the oAuth2 bearer access token method for a twitter feed. I followed correctly the details on the twitter developper website but i keep getting "Unable to verify your credentials twitter"...

If i copy my credentials over to my dev server (5.3.10 machine) i can actually login, get the token, retrieve the tweets, but on another client's server (5.2.17 machine) i can't get it to work with the exact same code.

Anything strikes you in the code below about the PHP differences? I believe it must have to be something to do with the file_get_content https stream wrapper options, but i can't find anything in the doc about it. (Openssl support is installed on the client server)

$twitter_bearer_access_token = get_option('twitter_bearer_access_token', null);
if($twitter_bearer_access_token == null)
{

    //Request a bearer access token
    $encodedAccessToken = base64_encode(TWITTER_CONSUMER_KEY.':'.TWITTER_CONSUMER_SECRET);

    //Setup the stream context options and create the stream to POST to twitter
    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Authorization: Basic '.$encodedAccessToken,
            'content' => 'grant_type=client_credentials',
        ),
    );
    $context  = stream_context_create($options);
    $result = json_decode(@file_get_contents('https://api.twitter.com/oauth2/token', false, $context));
    if(isset($result->token_type) && $result->token_type == 'bearer')
    {
        $twitter_bearer_access_token = $result->access_token;
        update_option('twitter_bearer_access_token', $twitter_bearer_access_token);
    }
    else
    {
        $twitter_bearer_access_token = false;
    }

}

Edit #1

I confirm in advance that all variables are the same on both servers, i've outputted and compared them. Only the file_get_contents return a different result.

Also, I tried copying the bearer access token gotten on the 5.3 server to the 5.2 server and the values still match but i keep getting errors about invalid authentication...

Edit #2

More info, it doesn't seem to have anything to do with the headers i send, i added practically all headers except the ones that don't pertain to me such as proxy header. It doesn't have anything to do with PHP being 5.2 or 5.3, tested on our staging server host and switch to 5.2 and still works...

Was it helpful?

Solution

Seems that using cURL works fine. Considering we want to limit our dependencies, we'll just have to resort to having two versions of the code (or eventually use a swapable component to retrieve the distant data)

//Init curl
$request = curl_init();
curl_setopt($request, CURLOPT_SSLVERSION, 3);
curl_setopt($request, CURLOPT_URL, 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name='.TWITTER_ACCOUNT.'&count=100');
curl_setopt($request, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$twitter_bearer_access_token));
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);

$result = json_decode($content = curl_exec($request));
curl_close($request);

Thats it folks

PS: There still isn't any reason for this to fail, this is just a fallback!

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