Foreword

Trying to get a dial tone with Twitter’s new OAuth can be a frustrating and daunting task. Especially when it comes to the utter lack of proper documentation with regards to just connecting to Twitter using OAuth. This tutorial will walk you through the steps required to be able to make calls using Twitter’s OAuth in PHP.

Đang xem: Kết quả tìm kiếm từ khóa “defi coin price php 【qc90com】【link to home home here_qc90com】”

Getting Started

OK, first things first. You’ll need to have your web application registered with Twitter, as well as the associated username and password of the account. If you haven’t registered your application with Twitter yet, here’s what you’ll need to do:

First visit http://dev.twitter.com/

Click on the “Register an app” link.

Then fill out all the appropriate information requested. Make sure that you’ve selected “Browser” as your “Application Type”.

You will also need to register a callback URL. This is the URL where people will be redirected to after they have authorized your website/application for use with their Twitter account. This is also where you will receive validation information directly from Twitter which will be required to make calls on behalf of the Twitter user.

Once you have filled out the form and registered your application, you’ll be presented with the details of your registration including your “Consumer Key” and “Consumer Secret”. You’ll be using those shortly so keep a browser instance open or copy them down.

*

Twitter Application Registration

Now that the prerequisites are done, it’s time to being the battle with OAuth.

Beginning OAuth

First let’s understand what needs to happen to get OAuth working.

The gist is simple enough, we need to create a header with authorization data in it. Post it to Twitter, get a token back from twitter letting us know we’re registered and everything is groovy. Next we’ll retrieve the authorization URL to allow users to authorize our application with their account and finally, do something…Uh Twittery, Twitter-esc…I don’t know, don’t ask.

Xem thêm: Làm Đầy Tháng Cho Bé Gái – Cúng Đầy Tháng Cho Bé Như Thế Nào

Now, the most important thing to remember here is Twitter is super picky about how everything is encoded…super picky. So if you make one mistake, you’ll get a forbidden and get rejected with very little help response message wise from Twitter.

So let’s start by getting the request token from Twitter, which will let us know we’re on the right track.

Getting The Request Token

To get the request token from Twitter we need to POST a call to:

https://api.twitter.com/oauth/request_token

But first we need to sign and encode an authorization header, and yes..it is a pain. Lucky for you I’ve already been through this so you don’t have to deal with figuring it all out. Here we go.

The authorization header required for the request token requires the following fields:

oauth_callback – the url to be redirected to after authorizationoauth_consumer_key – this is the consumer key you get after registering your application with Twitteroauth_nonce – this is a unique value that you generate to reduce the chance of someone hijacking your sessionoauth_signature_method – this is the method used to sign the base string, we’ll get to this in a bit, but for now the default value is “HMAC-SHA1”oauth_timestamp – this is the current timestamp.oauth_version – this is going to be “1.0”

An easy way to deal with the authorization header and it’s nuances is to load all the oauth header values into an associative array and pass them to functions that will sign, encode, etc.

$nonce = time();$timestamp = time();$oauth = array(“oauth_callback” => “http://yourdomain.com/callback_page”, “oauth_consumer_key” => “yourconsumerkey”, “oauth_nonce” => $nonce, “oauth_signature_method” => “HMAC-SHA1”, “oauth_timestamp” => $timestamp, “oauth_version” => “1.0”);Just to clarify what I’ve done so far. The $nonce variable can be pretty much whatever you want, I thought for the purpose of this tutorial however time() would be the easiest to understand. The $timestamp, well that should be pretty obvious. The $oauth is our associative array containing all the fields and values required to get us started.

Now that we have our oauth array, we need to create our base string. The base string is basically a signature of the action we want to perform which later we will sign using HMAC-SHA1 and our composite key. The result of which will be our oauth_signature. I know it sounds a bit confusing, but don’t worry. I’ll walk you through the entire process step by step.

Xem thêm:

So let’s build the base string. The base string has the format of:

METHOD&BASEURI&OAUTH_PARAMS_SORTED_AND_ENCODED

For example, here’s a fully encoded base string:

POST&https%3A%2F%2Fapi.twitter.com%2Foauth%2Frequest_token&oauth_callback%3Dhttp%253A%252F%252Flocalhost%253A3005%252Fthe_dance%252Fprocess_callback%253Fservice_provider_id%253D11%26oauth_consumer_key%3DGDdmIQH6jhtmLUypg82g%26oauth_nonce%3DQP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1272323042%26oauth_version%3D1.0

Yeah, it’s ugly. And here’s how you create it. As I said before Twitter is very picky about encoding, etc. So to ensure everything is encoded the same way each and every time, let’s use a function that will do it for use using our $oauth array.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *