Reputation: 572
My aim is to allow Twitter logins using twitter4j. I used this as reference.
The problem is that at line 64, it calls getAuthenticationURl()
. When I execute it takes me to the login page, instead of the page where I can allow/disallow my app access to the ppl's account.
However, if I change it to getAuthorizationUrl()
it takes to the correct page.
1) So what is the difference between the two?
2) This is the important question. I am providing the code below.
package com.example.twitter;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class WriteTweet extends Activity{
public final static String consumerKey = "xxxxxxxxxxxxxxxxxx";
public final static String consumerSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
private final String CALLBACKURL = "T4J_OAuth://callback";
Twitter twitter;
RequestToken requestToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
loginUser();
}
private void loginUser() {
// TODO Auto-generated method stub
try {
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(consumerKey, consumerSecret);
requestToken = twitter.getOAuthRequestToken(CALLBACKURL);
String authUrl = requestToken.getAuthorizationURL();
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse(authUrl)));
} catch (TwitterException ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
Log.e("in Main.OAuthLogin", ex.getMessage());
}
}
@Override
protected void onNewIntent(Intent intent) {
Log.e("hi","hi");
super.onNewIntent(intent);
Uri uri = intent.getData();
try {
String verifier = uri.getQueryParameter("oauth_verifier");
AccessToken accessToken = twitter.getOAuthAccessToken(requestToken,
verifier);
String token = accessToken.getToken(), secret = accessToken
.getTokenSecret();
displayTimeLine(token, secret); //after everything, display the first tweet
} catch (TwitterException ex) {
Log.e("Main.onNewIntent", "" + ex.getMessage());
}
}
@SuppressWarnings("deprecation")
void displayTimeLine(String token, String secret) {
Log.e("display","timeline");
}
}
the manifest is
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.twitter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ListTweets" >
</activity>
<activity android:name=".WriteTweet">
<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="T4J_OAuth" android:host="callback" />
</intent-filter>
</activity>
<activity android:name=".ViewFollowers">
</activity>
</application>
</manifest>
The problem is that it redirects me to https://mobile.twitter.com/
and I cant figure out what am doing wrong. The problem exist no matter if whether getAuthenticationUrl()
or getAuthorizationUrl()
is used. I had provided a dummy url in the callback_url
field in Twitter.
If I didn't provide a callbackurl
in code and in the twitter page it will give me a pin number. But that is not what I want. I need my twitter to redirect to my app. How can I achieve this?
Upvotes: 4
Views: 8618
Reputation: 572
This is the way I got around the problem,
create a web-view and load the conformation page of twitter from within your app.
vw = (WebView)findViewById(R.id.loginView);
vw.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
}); vw.loadUrl(authUrl);
Once you receive a callback_url, get the ' oauth_verifier '
Uri uri = Uri.parse(url);
String verifier = uri.getQueryParameter("oauth_verifier");
Use this verifier to get access and do what you want. In my case I destroy the webview and load the activity I require.
Upvotes: 3
Reputation: 22291
Use below URL as a Callback URL.
public static final String CALLBACK_URL = "x-oauthflow-twitter://callback";
and see below link for more information about twitter integration in android application.
Upvotes: 2