0

Hello I am trying to make a twitter API. I am using oAuth for it to authenticate myself. The problem is it doesnt do anything i got the same as in the guide which im following. Am i making a mistake in the configuration or in my code?

<?php require("src/TwitterOAuth.php"); session_start(); $twitteroauth = new TwitterOAuth("CONSUMER KEY", "CONSUMER SECRET KEY"); $request_token = $twitteroauth->getRequestToken("http://127.0.0.1:22119/twitter_login.php"); $_SESSION['oauth_token'] = $request_token['oauth_token']; $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; if($twitteroauth->http_code=200) { $url = $twitteroauth->getAuthorizeUrl($request_token['oauth_token']); header('Location:'. $url); } else { die('Something wrong happened.'); } ?> 

Here screenshot of my twitter app settings: http://i.imgur.com/De2XzWU.png

The guide im following: http://code.tutsplus.com/tutorials/how-to-authenticate-users-with-twitter-oauth--net-13595

Thanks in advance

0

1 Answer 1

1

first of all you need add new application to your twitter dev profile , and you need to specify the callback url , than if you're using a php mvc framework you need to go to your controller in twitter action you need to add the following:

 // TWITTER APP KEYS $consumer_key = 'something you get from twitter dev'; $consumer_secret = 'other thing you get from twitter dev'; // 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://something/otherthing" )); // 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']; 

if you want you can put some variables in the cookies for a father use :

 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); 

than you need to ask twitter to authorize your app

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

and the last thing you have to do is to render the url , basically this means that after the user give his approval we need to redirect him to another page where we could use more feature of twitter Oauth :

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

please take a look abraham's twitter Oauth it's possible to upload it using composer.json:

{ "type": "package", "package": { "name": "abraham/twitteroauth", "description": "Twitter oauth", "version": "dev-dev", "keywords": ["Twitter API", "Twitter oAuth"], "license": "MIT", "authors": [ { "name": "Abraham Williams", "email": "[email protected]" } ], "require": { "php": ">=5.3.2" }, "autoload": { "files": ["twitteroauth/OAuth.php"] }, "source": { "type": "git", "url": "https://github.com/abraham/twitteroauth", "reference": "origin/dev" } }}] 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.