About the author
Here's a quick primer on how I did my Twitter authentication on Android.
In a button event, invoke the OAuthLogin() method, where it is defined as:
private void OAuthLogin() { if (Utility.getAccessToken(this) == null) { try {// The uri given in getOAuthRequestToken has to match the manifest's. this.requestToken = Utility.getTwitter(this).getOAuthRequestToken("x://y"); startActivityForResult(new Intent("android.intent.action.VIEW", Uri.parse(this.requestToken.getAuthenticationURL())), Constants.ACTIVITY_LOGIN); } catch (TwitterException e) { Toast t = Toast.makeText(this, "Unable to login now, please try about 15 minutes later. Reason: "+e.getMessage(), Toast.LENGTH_LONG); t.show(); Log.e("in OAuthLogin", e.getMessage()); } } }
The OAuthLogin method calls the default browser with the authentication URL, with the RequestToken passed along to it, so that after the authentication is completed, the browser calls back to the URI pointed to by the RequestToken, which will then be handled by the activity that has the specified URI pattern.
In the Android manifest,
<activity android:name="ActivityName" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <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="x" android:host="y" /> </intent-filter> </activity>
In the actiivity's onCreate method,
Intent intent = getIntent(); Uri uri = intent.getData(); Twitter t = Utility.getTwitter(this); if (uri!=null) { String oauthVerifier = uri.getQueryParameter("oauth_verifier"); try { AccessToken a = t.getOAuthAccessToken(oauthVerifier); Utility.storeAccessToken(this, a); } catch (TwitterException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Hope this helps everyone who's looking for Twitter4J authentication on Android. If this helps you, please drop a comment.
How to free more space on your home drive by redirecting the location for SDKs in RAD Studio