Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Questions regarding use of Twitter-Oauth API in php

Recently I read a nice tutorial How to Authenticate Users With Twitter OAuth even it written before changing twitter ID format but it works with the new Twitter ID Format too.

I have some questions , please explain if anybody done it successfully..

  • Why we always call two method getRequestToken and getAccessToken ? Is it to get access token and access token secret ? but both are already given at below page...

    http://dev.twitter.com/apps/{your_app_id}/my_token.

    what is the exactly need of request token and request token secret?? although i notice that both token comes different each time we process.

  • if we update our status from below method

    $connection->post( 'statuses/update', array('status' => 'some message got from text area value' );

Then how do we verify that status has been updated successfully?? It means if i want to display alert message post has been sent successfully, how do i implement that in our PHP page??

  • which callback URL is important, i.e. where actually user is navigated after posting or doing stuff on twitter?

    1. is it the URL Registered OAuth Callback URL which is written at the time of developing an application on

      http://dev.twitter.com/apps/{id_no}
      or

    2. is it the URL which is defined in our php code (config.php) like

      define('OAUTH_CALLBACK', 'http://www.xyz.com');

    one more Q'n

  • How to handle deny the access of applications?
    Note: Please refer my Question regarding this

update for @Thai

i did below according to your suggestion

$user_info = $connection->get('account/verify_credentials');
$status_info =$connection->get('statuses/show/', array('id' =>32320907720523776) );

echo "<pre>";
print_r($status_info);

echo "</pre> Content : <pre>";
print_r($user_info);

returns below

stdClass Object
(
    [request] => /1/statuses/show.json?id=3.2320907720524E%2B16&oauth_consumer_key=jashak..&oauth_nonce=bec...&oauth_signature=%2FMj%2B0Z7oyYNKdMn%2B%2FOJ6Ba8ccfo%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1296541384&oauth_token=223961574-cW4...&oauth_version=1.0a
    [error] => No status found with that ID.
)

note: i hide the oauth_consumer key,oauth_nonce and oauth_token for security purpose ;)

Content:
stdClass Object
(
    [follow_request_sent] => 
    [profile_link_color] => 038543
    [profile_image_url] => http://a3.twimg.com/profile_images/1215444646/minialist-photography-9_normal.jpg
    [contributors_enabled] => 
    [favourites_count] => 31
    [profile_sidebar_border_color] => EEEEEE
    [id_str] => 223961574 // this is also id_str
    [status] => stdClass Object
        (
            [retweeted] => 
            [id_str] => 32320907720523776 // this id_str i used
            [in_reply_to_status_id_str] => 
            [geo] => 
            [contributors] => 
            [source] => Black Noise
            [in_reply_to_user_id_str] => 
            [retweet_count] => 0
            [truncated] => 
            [coordinates] => 
            [created_at] => Tue Feb 01 06:14:39 +0000 2011
            [favorited] => 
            [text] => Twitter test: verify that status has been updated
            [place] => 
            [in_reply_to_screen_name] => 
            [in_reply_to_status_id] => 
            [id] => 3.2320907720524E+16
            [in_reply_to_user_id] => 
        )
   [screen_name] => ltweetl
   [profile_use_background_image] => 1
   ....
   ...

i got error No status found with that ID and which id_str u r mentioning??

like image 929
diEcho Avatar asked Jan 21 '11 06:01

diEcho


People also ask

Which authentication mechanism does Twitter use for authenticating applications?

Most of Twitter's Enterprise APIs require HTTP Basic Authentication. This consists of a valid email address and password combination passed as an authorization header for each API request.

Do Twitter API keys expire?

These tokens do not expire but can be revoked by the user at any time. Twitter allows you to obtain user access tokens through the 3-legged OAuth flow, which allows your application to obtain an access token and access token secret by redirecting a user to Twitter and having them authorize your application.

Is consumer key same as API key Twitter?

The API Key and Secret (also known as Consumer Key and Secret) are the most fundamental credentials required to access the Twitter API. These credentials act as the username and password for your Twitter App, and are used by the Twitter API to understand which App requests are coming from.


1 Answers

This answer is not specifically for Abraham's Twitter-OAuth API, but applies to Twitter's OAuth API in general.

  1. That page only gives you your access token and access token secret to your own apps. This is fine if you don't need your app to be authenticated as any other users, so no need to request request tokens and no need to exchange the request token for access token, you just use your access token

    However, if you want to authenticate as other users, you will have to go through all the required steps, which is explained in short here:

    • You request the request token. It is only used for signing in, and cannot be used to access user's data. You will get the request token and request token secret. When requesting the request token, you can specify a callback URL which Twitter will send your users to when it is authenticated successfully.
    • You need to keep the request token and secret until the authentication is done.
    • After that, you redirect the user to http://api.twitter.com/oauth/authorize?oauth_token= followed by OAuth token.
    • After the user signed in to Twitter and allowed your application, Twitter sends the user back to the callback URL.
    • You exchange the request token for the access token, and you can then discard the request token because you won't need it anymore.

    You can keep the access token as long as you need it. If you don't keep the access token, you will need to request the request token and exchange it for access token again, and your users will have to sign in again.

    So basically, if you are creating something that the user needs to sign in using Twitter, you need to do all the steps above to get the user signed in. If you are just using Twitter's API for yourself, you don't need the authentication step. Use your access token.

  2. You can check for the tweet's ID by checking for the id_str key on the returned response.

    If the status isn't posted, Twitter will return an error object.

  3. You can specify the default OAuth callback in the application settings page, which will be used when you don't explicitly specify a callback.

    This is required because if you somehow forget to or did not specify a callback URL, Twitter will still know where to redirect your users to.

    However, Twitter encourages that you should explicitly specify a callback URL. There are many benefit from using a callback URL, such as being able to specify any URLs as the callback. I used to benefit from this one because my Twitter client runs on two different domains, I could redirect the users back to the right place.

like image 131
Thai Avatar answered Sep 29 '22 04:09

Thai