How to integrate Oauth2 Twitter API?

Twitter is one of the most powerful social network platform to promote your application. With Twitter API, you can make users follow, tweet, DM and much more directly from your website without opening twitter.

In this article, we will learn about how to integrate Twitter API in your web application.

To start with Twitter API, first create your Twitter developer account. Then create a new Application. Now you can get Consumer API keys from the generated Application in Keys and tokens tab. Don't forget to set appropriate permissions also from the permission tab. Here is the Twitter API request flow.

Get bearer token

Now from your code side, to create any Twitter API curl request, first create a new curl request to get bearer token and pass it in request header.
To get bearer token, first encode consumer key and consumer secret in RFC 1738 method. Then from consumers credential now encode them in base64 format.

RFC-1738-encoded-consumer-key:RFC-1738-encoded-consumer-secret

And you will get string like this:

eHZ6MWV2RlM0d0VFUFRHRUZQSEJvZzpMOHFxOVBaeVJnNmllS0dFS2hab2xHQzB2SldMdzhpRUo4OERSZHlPZw==

Now you all you need to do is get bearer token with post CURL request with https://api.twitter.com/oauth2/token?grant_type=client_credentials this URL. Be sure to set header with

Authorization: Basic bearer-token-credentials
Content-Type: application/x-www-form-urlencoded
Content-Length: 29


and you will get Bearer Token in bellow json format

{
    "token_type": "bearer",
    "access_token": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%2FAAAAAAAAAAAAAAAAAAAA%3DAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
}

Here is my request code in PHP language:

<?php

$consumer_api_key = rawurldecode('xvz1evFS4wEEPTGEFPHBog');
$consumer_api_secret = rawurldecode('L8qq9PZyRg6ieKGEKhZolGC0vJWLw8iEJ88DRdyOg');

$base64_key = base64_encode($consumer_api_key.':'.$consumer_api_secret);

$url = 'https://api.twitter.com/oauth2/token?grant_type=client_credentials';

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_HTTPHEADER => array(
        "authorization: Basic ".$base64_key,
        "cache-control: no-cache",
        "content-type: application/x-www-form-urlencoded"
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

?>

Create Twitter API request

Now you have get access_teken which you can pass in header request in any Twitter API. Here is all lists of Twitter API.

For example, if you want to get User data by screen name or id, just send GET request in https://api.twitter.com/1.1/users/show.json?screen_name=here-comes-twitter-screen-name URL.

Here  is my request code in PHP:

<?php

$url = 'https://api.twitter.com/1.1/users/show.json?screen_name=twitterdev';
$access_token = 'AAAAAAAAAAAAAAAAAAAAAJEvBgEAAAAALuTinPbWuLuDuWA0NRr7ZE%3DCNtMfu1gXJYW8MQzAOBnlIqUz73oIWuRFjUSetGNFN3cYCANj8';

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "authorization: Bearer ".$access_token,
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

?>

Invalidate access_token

You can also invalidate the generated token by the following request.

<?php

$consumer_api_key = rawurldecode('xvz1evFS4wEEPTGEFPHBog');
$consumer_api_secret = rawurldecode('L8qq9PZyRg6ieKGEKhZolGC0vJWLw8iEJ88DRdyOg');

$base64_key = base64_encode($consumer_api_key.':'.$consumer_api_secret);
$access_token = '$access_token';

$url = 'https://api.twitter.com/oauth2/invalidate_token?access_token='.$access_token;

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_HTTPHEADER => array(
        "authorization: Basic ".$base64_key,
        'Accept: */*',
        'Content-Length: 119',
        'Content-Type: application/x-www-form-urlencoded'
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

?>

Conclusion

This way, you can create all Twitter API request from the access_token. I hope this article will help new developers to use Twitter API. Thank you all and have a happy twitting.

 
 
 
 
 
 
 
 
Tags: