使用 twitter4j 回调登录 [英] Log in with twitter4j callback
问题描述
我尝试编写一个应用程序,用户可以在其中登录 Twitter.我像库一样使用 twitter4j.我的问题是,当我进入必须输入用户名和密码的页面时,程序块因为我不知道使用回调来进入我的应用程序.有人可以帮我吗?
I try to write an application where an user can log in on twitter. I use twitter4j like library.My problem is that when I go in the page where I must put username and password, the program block because i don't know use callback to came in my application. Someone can me help?
公共类 MainActivity 扩展 Activity {
public class MainActivity extends Activity {
private Twitter twitter;
RequestToken requestToken;
final public static String CALLBACK_SCHEME = "x-latify-oauth-twitter";
final public static String CALLBACK_URL = CALLBACK_SCHEME + "://callback";
private Uri uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new updateTwitterStatus().execute();
}
});
}
@Override
protected void onDestroy() {
twitter.shutdown();
}
class updateTwitterStatus extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String testStatus = "prova tweet ";
ConfigurationBuilder cb = new ConfigurationBuilder();
// the following is set without accesstoken- desktop client
cb.setDebugEnabled(true)
.setOAuthConsumerKey("******")
.setOAuthConsumerSecret(
"*****");
try {
TwitterFactory tf = new TwitterFactory(cb.build());
twitter = tf.getInstance();
Log.i("bauu", "miao");
requestToken = twitter.getOAuthRequestToken();
String authUrl = requestToken.getAuthenticationURL();
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(requestToken.getAuthenticationURL())));
uri = Uri.parse(requestToken.getAuthenticationURL());
return authUrl;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(s)));
}
}
推荐答案
确保你在 twitter dev app options 中的回调 URL 如下,
Make sure your callback URL in twitter dev app options are as follows,
http://YOUR-URL/app://YOUR-APP-HOST
在您的 android 清单文件中,在将您带到 twitter 的活动之间,确保您定义:
and within your android manifest file, in between the of the actvitiy that takes you to twitter, make sure you define:
<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="YOUR-APP-HOST"
android:scheme="app" />
</intent-filter>
最后,请确保在您的程序中,
lastly, make sure in your program,
final public static String CALLBACK_URL = "app://YOUR-APP-HOST";
这篇关于使用 twitter4j 回调登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!