I have to implement the oAuth2 application auth method for a twitter feed on a web site. I followed the procedure carefuly and it works on my dev server and on my managed VPS. Problem is, if i copy the code to other servers such as other clients still on old machines (Featuring PHP 5.2 for instance), it starts behaving incorrectly and returns either 403s or “Unable to verify your credentials”…
If i copy the other client’s credentials over to my dev server (5.3.10 machine) i can actually login, get the token, retrieve the tweets, but on the client’s server (5.2.17 machine) i can’t get it to work with the exact same code.
The code itself is pretty simple and relies of a few methods from Wordpress, all variables and values work fine, like i said, the code itself works, it just changes behavior on different machines:
$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;
}
}