Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get user Access Token and Access Secret with the Twitter API using php

Tags:

php

oauth

twitter

I'm trying to code a couple of PHP pages for getting users tokens the Twitter API 1.1. I’m using the TwitterOAuth library https://twitteroauth.com/

First page: twitter-go.php

The user opens it and gets redirected to twitter.com for authorizing the app.

I'm guessing this is where the POST oauth/request_token and GET oauth/authorize functions are being used.

Second page: twitter-back.php

The user gets redirected there from twitter once he authorizes the app. It then displays the user Access Token and the user Access Secret (or store them into a database for later use).

I'm guessing this is where the POST oauth/access_token function is being used.

Is this the correct way of getting a user Secret Token and Access Token?

like image 520
Arthur Avatar asked Feb 16 '15 19:02

Arthur


1 Answers

Alright, actually managed to figure it out myself. Here is my code for those who need it:

First page: twitter-go.php

The user opens it and gets redirected to twitter.com for authorizing the app.

<?php

//LOADING LIBRARY
require "twitteroauth/autoloader.php";
use Abraham\TwitterOAuth\TwitterOAuth;

//TWITTER APP KEYS
$consumer_key = 'yourkey';
$consumer_secret = 'yourkey';

//CONNECTION TO THE TWITTER APP TO ASK FOR A REQUEST TOKEN
$connection = new TwitterOAuth($consumer_key, $consumer_secret);
$request_token = $connection->oauth("oauth/request_token", array("oauth_callback" => "http://boulangerie-colas.fr/twitter/twitter-back.php"));
//callback is set to where the rest of the script is

//TAKING THE OAUTH TOKEN AND THE TOKEN SECRET AND PUTTING THEM IN COOKIES (NEEDED IN THE NEXT SCRIPT)
$oauth_token=$request_token['oauth_token'];
$token_secret=$request_token['oauth_token_secret'];
setcookie("token_secret", " ", time()-3600);
setcookie("token_secret", $token_secret, time()+60*10);
setcookie("oauth_token", " ", time()-3600);
setcookie("oauth_token", $oauth_token, time()+60*10);

//GETTING THE URL FOR ASKING TWITTER TO AUTHORIZE THE APP WITH THE OAUTH TOKEN
$url = $connection->url("oauth/authorize", array("oauth_token" => $oauth_token));

//REDIRECTING TO THE URL
header('Location: ' . $url);

?>

Second page: twitter-back.php

The user gets redirected there from twitter once he authorizes the app. It then displays the user Access Token and the user Access Secret.

<?php 
/**
 * users gets redirected here from twitter (if user allowed you app)
 * you can specify this url in https://dev.twitter.com/ and in the previous script
 */ 

//LOADING LIBRARY
require "twitteroauth/autoloader.php";
use Abraham\TwitterOAuth\TwitterOAuth;

//TWITTER APP KEYS
$consumer_key = 'yourkey';
$consumer_secret = 'yourkey';

//GETTING ALL THE TOKEN NEEDED
$oauth_verifier = $_GET['oauth_verifier'];
$token_secret = $_COOKIE['token_secret'];
$oauth_token = $_COOKIE['oauth_token'];

//EXCHANGING THE TOKENS FOR OAUTH TOKEN AND TOKEN SECRET
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $token_secret);
$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $oauth_verifier));

$accessToken=$access_token['oauth_token'];
$secretToken=$access_token['oauth_token_secret'];

//DISPLAY THE TOKENS
echo "<b>Access Token : </b>".$accessToken."<br />";
echo "<b>Secret Token : </b>".$secretToken."<br />";

 ?>

Please remember that you need to be using the using the TwitterOAuth library https://twitteroauth.com/

like image 149
Arthur Avatar answered Oct 31 '22 12:10

Arthur