Search code examples
javaandroidtwitter-oauthspring-social-twitterspring-android

Spring Android Twitter Callback URL


I need to implement an OAuth login in my Android application using the Twitter API. I found a great project on GitHub (spring-android-samples) with a very good example, but I have a problem with respect to the parameter "Callback URL".

When I submit my login and password, my android application opens a browser instance (with callback URL) rather than processing the response in the background:

enter image description here

Has anyone had to implement something similar and could help me please? Thank you!


Solution

  • You don't actually have to register the x-android-org-springsource-twitterclient://twitter-oauth-response URL with the Twitter app settings. In fact, the callback URL configured on Twitter doesn't really matter in this case. The callback URL is sent to Twitter within the request to fetch the OAuth request token.

    Note that within the AndroidManifest.xml there is a specified <intent-filter> for the TwitterWebOAuthActivity which configures this callback URL. This filter tells Android to redirect requests to the activity.

    <activity
        android:name=".TwitterWebOAuthActivity"
        android:excludeFromRecents="true"
        android:noHistory="true" >
        <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:host="twitter-oauth-response"
                android:scheme="x-org-springsource-android-twitterclient" />
        </intent-filter>
    </activity>
    

    Note also in res/values/connect_settings.xml the twitter_oauth_callback_url string is again configured.

    <string name="twitter_oauth_callback_url">x-org-springsource-android-twitterclient://twitter-oauth-response</string>
    

    This string is used in TwitterWebOAuthActivity to fetch the Twitter request token.

    @Override
    protected OAuthToken doInBackground(Void... params) {
        // Fetch a one time use Request Token from Twitter
        return connectionFactory.getOAuthOperations().fetchRequestToken(getOAuthCallbackUrl(), null);
    }
    

    If you want to modify the callback URL for your own app, you need to make sure it is configured the same in the AndroidManifest.xml and res/values/connect_settings.xml.