Pooja M. Bohora
Pooja M. Bohora

Reputation: 1331

Callback Url for twitter

I am creating an android application in which I have to integrate twitter for uploading picture on it using twitter4j library.

I am not able to specify callback Url on twitter for application.

Please help me.

Upvotes: 2

Views: 4629

Answers (1)

Sushant
Sushant

Reputation: 1874

You should use intent filters to get callback in case of twitter as

requestToken = twitterFactory.getInstance()
                    .getOAuthRequestToken("oauth://com.example.twitter"); //note that "oauth" is your scheme name, "com.example.twitter" is your host name  on your intent-filter

To the activity where you want to get you callback after authorization add following intent filter

<activity android:name=".TwitterActivity"
              android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="oauth" android:host="com.example.twitter"  />
    </intent-filter>
</activity>

And in your Activity where you want to get callback to (in this case TwitterActivity) get your verifier as

Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith("oauth://com.example.twitter")) {
    String verifier = uri.getQueryParameter("oauth_verifier");
    // request for access token using requestToken and verifier which are used to login second time
}

Here important thing to notice is

-> first of all you ask for request token by calling web api

-> that request token can used to authorize your user

-> after authorization you browser loads certain Url which can start your activity as you have added intent-filter to your activity with corresponding host name and scheme name (i.e. host = "oauth", scheme="com.example.twitter")

-> you can get you access tokens from the loaded Url i.e. extracting verifier from it and using your request token

for detail code please visit https://bitbucket.org/hintdesk/android-how-to-tweet-in-twitter-within-android-client/src/f8923a4a641313cae7243da51530b472730c2439?at=default

Upvotes: 1

Related Questions