使用 oauth 和 twitter4j 集成 Android Twitter
Posted
技术标签:
【中文标题】使用 oauth 和 twitter4j 集成 Android Twitter【英文标题】:Android Twitter integration using oauth and twitter4j 【发布时间】:2013-07-04 05:01:16 【问题描述】:我想将 twitter 集成到一个 android 应用程序中,并找到了很多教程。实现了其中的 2 个。但是在实现之后,当运行应用程序时,我才知道他们使用的是旧版本的twitter4J 库。
虽然有很多其他教程可用,但它们都不是最新的。即只大2-3个月。我需要一个教程或示例,它使用最新的twitter4J
库版本,即twitter4j-core-3.0.3。
我的主要目标是允许用户在他/她的帐户上发布tweets
。但同样,如果用户没有登录,我首先需要询问凭据。另外,如果用户点击logout
按钮,那么我需要一些方法来注销用户。
【问题讨论】:
推特使用OAuth 1.0A @brillenheini 感谢您让我知道这一点。我会相应地编辑我的问题。 跟随本教程tech-papers.org/integrate-twitter-with-android-application Vicky 先生的链接好像坏了 【参考方案1】:我解决了这个问题。我对在教程中找到的代码进行了更改以使其正常工作。在此处复制整个代码。只需替换为您的ConsumerKey
和ConsumerSecret
。
您需要将twitter4j 库添加到项目的libs
文件夹中。
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidhive.twitterconnect"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<!-- Permission - Internet Connect -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Network State Permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
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>
<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="t4jsample"
android:scheme="oauth" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java:
package com.androidhive.twitterconnect;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;
import com.androidhive.twitterconnect.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
// Constants
/**
* Register your here app https://dev.twitter.com/apps/new and get your
* consumer key and secret
* */
static String TWITTER_CONSUMER_KEY = "PutYourConsumerKeyHere"; // place your cosumer key here
static String TWITTER_CONSUMER_SECRET = "PutYourConsumerSecretHere"; // place your consumer secret here
// Preference Constants
static String PREFERENCE_NAME = "twitter_oauth";
static final String PREF_KEY_OAUTH_TOKEN = "oauth_token";
static final String PREF_KEY_OAUTH_SECRET = "oauth_token_secret";
static final String PREF_KEY_TWITTER_LOGIN = "isTwitterLogedIn";
static final String TWITTER_CALLBACK_URL = "oauth://t4jsample";
// Twitter oauth urls
static final String URL_TWITTER_AUTH = "auth_url";
static final String URL_TWITTER_OAUTH_VERIFIER = "oauth_verifier";
static final String URL_TWITTER_OAUTH_TOKEN = "oauth_token";
// Login button
Button btnLoginTwitter;
// Update status button
Button btnUpdateStatus;
// Logout button
Button btnLogoutTwitter;
// EditText for update
EditText txtUpdate;
// lbl update
TextView lblUpdate;
TextView lblUserName;
// Progress dialog
ProgressDialog pDialog;
// Twitter
private static Twitter twitter;
private static RequestToken requestToken;
private AccessToken accessToken;
// Shared Preferences
private static SharedPreferences mSharedPreferences;
// Internet Connection detector
private ConnectionDetector cd;
// Alert Dialog Manager
AlertDialogManager alert = new AlertDialogManager();
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
cd = new ConnectionDetector(getApplicationContext());
// Check if Internet present
if (!cd.isConnectingToInternet())
// Internet Connection is not present
alert.showAlertDialog(MainActivity.this, "Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
// Check if twitter keys are set
if(TWITTER_CONSUMER_KEY.trim().length() == 0 || TWITTER_CONSUMER_SECRET.trim().length() == 0)
// Internet Connection is not present
alert.showAlertDialog(MainActivity.this, "Twitter oAuth tokens", "Please set your twitter oauth tokens first!", false);
// stop executing code by return
return;
// All UI elements
btnLoginTwitter = (Button) findViewById(R.id.btnLoginTwitter);
btnUpdateStatus = (Button) findViewById(R.id.btnUpdateStatus);
btnLogoutTwitter = (Button) findViewById(R.id.btnLogoutTwitter);
txtUpdate = (EditText) findViewById(R.id.txtUpdateStatus);
lblUpdate = (TextView) findViewById(R.id.lblUpdate);
lblUserName = (TextView) findViewById(R.id.lblUserName);
// Shared Preferences
mSharedPreferences = getApplicationContext().getSharedPreferences(
"MyPref", 0);
/**
* Twitter login button click event will call loginToTwitter() function
* */
btnLoginTwitter.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View arg0)
// Call login twitter function
loginToTwitter();
);
/**
* Button click event to Update Status, will call updateTwitterStatus()
* function
* */
btnUpdateStatus.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// Call update status function
// Get the status from EditText
String status = txtUpdate.getText().toString();
// Check for blank text
if (status.trim().length() > 0)
// update status
new updateTwitterStatus().execute(status);
else
// EditText is empty
Toast.makeText(getApplicationContext(),
"Please enter status message", Toast.LENGTH_SHORT)
.show();
);
/**
* Button click event for logout from twitter
* */
btnLogoutTwitter.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View arg0)
// Call logout twitter function
logoutFromTwitter();
);
/** This if conditions is tested once is
* redirected from twitter page. Parse the uri to get oAuth
* Verifier
* */
if (!isTwitterLoggedInAlready())
Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL))
// oAuth verifier
final String verifier = uri
.getQueryParameter(URL_TWITTER_OAUTH_VERIFIER);
try
Thread thread = new Thread(new Runnable()
@Override
public void run()
try
// Get the access token
MainActivity.this.accessToken = twitter.getOAuthAccessToken(
requestToken, verifier);
catch (Exception e)
e.printStackTrace();
);
thread.start();
// Shared Preferences
Editor e = mSharedPreferences.edit();
// After getting access token, access token secret
// store them in application preferences
e.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken());
e.putString(PREF_KEY_OAUTH_SECRET,
accessToken.getTokenSecret());
// Store login status - true
e.putBoolean(PREF_KEY_TWITTER_LOGIN, true);
e.commit(); // save changes
Log.e("Twitter OAuth Token", "> " + accessToken.getToken());
// Hide login button
btnLoginTwitter.setVisibility(View.GONE);
// Show Update Twitter
lblUpdate.setVisibility(View.VISIBLE);
txtUpdate.setVisibility(View.VISIBLE);
btnUpdateStatus.setVisibility(View.VISIBLE);
btnLogoutTwitter.setVisibility(View.VISIBLE);
// Getting user details from twitter
// For now i am getting his name only
long userID = accessToken.getUserId();
User user = twitter.showUser(userID);
String username = user.getName();
// Displaying in xml ui
lblUserName.setText(Html.fromHtml("<b>Welcome " + username + "</b>"));
catch (Exception e)
// Check log for login errors
Log.e("Twitter Login Error", "> " + e.getMessage());
e.printStackTrace();
/**
* Function to login twitter
* */
private void loginToTwitter()
// Check if already logged in
if (!isTwitterLoggedInAlready())
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
Configuration configuration = builder.build();
TwitterFactory factory = new TwitterFactory(configuration);
twitter = factory.getInstance();
Thread thread = new Thread(new Runnable()
@Override
public void run()
try
requestToken = twitter
.getOAuthRequestToken(TWITTER_CALLBACK_URL);
MainActivity.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse(requestToken.getAuthenticationURL())));
catch (Exception e)
e.printStackTrace();
);
thread.start();
else
// user already logged into twitter
Toast.makeText(getApplicationContext(),
"Already Logged into twitter", Toast.LENGTH_LONG).show();
/**
* Function to update status
* */
class updateTwitterStatus extends AsyncTask<String, String, String>
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute()
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Updating to twitter...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
/**
* getting Places JSON
* */
protected String doInBackground(String... args)
Log.d("Tweet Text", "> " + args[0]);
String status = args[0];
try
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
// Access Token
String access_token = mSharedPreferences.getString(PREF_KEY_OAUTH_TOKEN, "");
// Access Token Secret
String access_token_secret = mSharedPreferences.getString(PREF_KEY_OAUTH_SECRET, "");
AccessToken accessToken = new AccessToken(access_token, access_token_secret);
Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);
// Update status
twitter4j.Status response = twitter.updateStatus(status);
Log.d("Status", "> " + response.getText());
catch (TwitterException e)
// Error in updating status
Log.d("Twitter Update Error", e.getMessage());
e.printStackTrace();
return null;
/**
* After completing background task Dismiss the progress dialog and show
* the data in UI Always use runOnUiThread(new Runnable()) to update UI
* from background thread, otherwise you will get error
* **/
protected void onPostExecute(String file_url)
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable()
@Override
public void run()
Toast.makeText(getApplicationContext(),
"Status tweeted successfully", Toast.LENGTH_SHORT)
.show();
// Clearing EditText field
txtUpdate.setText("");
);
/**
* Function to logout from twitter
* It will just clear the application shared preferences
* */
private void logoutFromTwitter()
// Clear the shared preferences
Editor e = mSharedPreferences.edit();
e.remove(PREF_KEY_OAUTH_TOKEN);
e.remove(PREF_KEY_OAUTH_SECRET);
e.remove(PREF_KEY_TWITTER_LOGIN);
e.commit();
// After this take the appropriate action
// I am showing the hiding/showing buttons again
// You might not needed this code
btnLogoutTwitter.setVisibility(View.GONE);
btnUpdateStatus.setVisibility(View.GONE);
txtUpdate.setVisibility(View.GONE);
lblUpdate.setVisibility(View.GONE);
lblUserName.setText("");
lblUserName.setVisibility(View.GONE);
btnLoginTwitter.setVisibility(View.VISIBLE);
/**
* Check user already logged in your application using twitter Login flag is
* fetched from Shared Preferences
* */
private boolean isTwitterLoggedInAlready()
// return twitter login status from Shared Preferences
return mSharedPreferences.getBoolean(PREF_KEY_TWITTER_LOGIN, false);
protected void onResume()
super.onResume();
AlertDialogManager.java:
package com.androidhive.twitterconnect;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
public class AlertDialogManager
/**
* Function to display simple Alert Dialog
* @param context - application context
* @param title - alert dialog title
* @param message - alert message
* @param status - success/failure (used to set icon)
* - pass null if you don't want icon
* */
public void showAlertDialog(Context context, String title, String message,
Boolean status)
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
if(status != null)
// Setting alert dialog icon
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int which)
);
// Showing Alert Message
alertDialog.show();
ConnectionDetector.java:
package com.androidhive.twitterconnect;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetector
private Context _context;
public ConnectionDetector(Context context)
this._context = context;
/**
* Checking for all possible internet providers
* **/
public boolean isConnectingToInternet()
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
return true;
return false;
这里是 Ravi Tamada 的original code。我所做的更改仅在 MainActivity.java
和 AndroidManifest.xml
文件中。
【讨论】:
你好@akash 你解决了你的问题吗?我使用了 twitter4j 2.3.5 库,其中包含向另一个用户发送推文的方法。如果您还没有解决您的问题,请回复。抱歉回复晚了谢谢。 我试过这段代码,访问令牌返回空请帮助 @Ravi 确保您已在 dev.twitter.com 创建访问令牌。如果您已经尝试删除它们并重新创建。还要检查您的使用者密钥和使用者密码是否已设置。 实际上我解决了问题是因为上面的代码序列,访问令牌是在一个单独的线程中获取的,并且它被保存在主线程中的共享首选项中,因为来自单独线程的代码将在主线程中的代码不固定之后或之前运行,在我的情况下,主线程中的代码在它获取访问令牌之前运行,这就是它为空的原因,所以我将它更改为 asynctask .. 但无论如何都感谢 Ravi Tamada 代码不起作用!请参阅关于错误的评论中的讨论!我收到此错误 01-26 13:02:25.839: E/Twitter Login Error(2022): > null 01-26 13:02:25.839: W/System.err(2022): java.lang.NullPointerException 01- 26 13:02:25.839: W/System.err(2022): 在 com.androidhive.twitterconnect.MainActivity.onCreate(MainActivity.java:200) 01-26 13:02:25.839: W/System.err(2022) : 在 android.app.Activity.performCreate(Activity.java:5243) 01-26 13:02:25.839: W/System.err(2022): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 【参考方案2】:我找到了一个很好的例子,它适用于 android 上的 twitter4j 3.0.3。其他人不工作。 http://hintdesk.com/how-to-tweet-in-twitter-within-android-client/
【讨论】:
链接不是答案。而且该链接甚至没有提供完整的代码。如果您能够使其正常工作,请发布代码。 @rcompton 该链接是一个完整的教程。无需在此处复制和粘贴所有内容。它没有提供完整的项目,但确实提供了核心代码。学了几天Android编程的人应该都明白了。我用谷歌搜索并尝试了许多示例,但所有示例都无法正常工作。也许那是因为 twitter 将 api 更改为 oauth v1.1 并且这些示例没有更新。此示例是唯一适用于 android 上的 twitter4j 3.0.3 的示例。【参考方案3】:为了说明我上面的评论,这是我最后的工作 MainActivity 类。 与上面的代码非常相似,不同之处在于:
内部类OAuthAccessTokenTask
,包括获取 Oauth 令牌和用户信息
它的回调onRequestTokenRetrieved(Exception)
另外,请注意,要使其正常工作,您必须在 twitter 应用设置中声明回调 url,即使是虚假的。我花了几个小时才弄清楚它的工作原理。
当您查看 twitter4j 文档时,前几段代码指的是您必须从授权网页获取的 PIN 码。这就是在您的应用中未设置回调 url 时发生的情况。它被称为基于 PIN 的身份验证,您不想在移动设备上使用它:)
public class MainActivity extends Activity
// Constants
/**
* Register your here app https://dev.twitter.com/apps/new and get your
* consumer key and secret
* */
static String TWITTER_CONSUMER_KEY = "PutYourConsumerKeyHere"; // place your cosumer key here
static String TWITTER_CONSUMER_SECRET = "PutYourConsumerSecretHere"; // place your consumer secret here
// Preference Constants
static String PREFERENCE_NAME = "twitter_oauth";
static final String PREF_KEY_OAUTH_TOKEN = "oauth_token";
static final String PREF_KEY_OAUTH_SECRET = "oauth_token_secret";
static final String PREF_KEY_TWITTER_LOGIN = "isTwitterLogedIn";
static final String TWITTER_CALLBACK_URL = "oauth://t4jsample";
// Twitter oauth urls
static final String URL_TWITTER_AUTH = "auth_url";
static final String URL_TWITTER_OAUTH_VERIFIER = "oauth_verifier";
static final String URL_TWITTER_OAUTH_TOKEN = "oauth_token";
// Login button
Button btnLoginTwitter;
// Update status button
Button btnUpdateStatus;
// Logout button
Button btnLogoutTwitter;
// EditText for update
EditText txtUpdate;
// lbl update
TextView lblUpdate;
TextView lblUserName;
// Progress dialog
ProgressDialog pDialog;
// Twitter
private static Twitter twitter;
private static RequestToken requestToken;
private AccessToken accessToken;
private User user;
// Shared Preferences
private static SharedPreferences mSharedPreferences;
// Internet Connection detector
private ConnectionDetector cd;
// Alert Dialog Manager
AlertDialogManager alert = new AlertDialogManager();
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
cd = new ConnectionDetector(getApplicationContext());
// Check if Internet present
if (!cd.isConnectingToInternet())
// Internet Connection is not present
alert.showAlertDialog(MainActivity.this, "Internet Connection Error", "Please connect to working Internet connection", false);
// stop executing code by return
return;
// Check if twitter keys are set
if(TWITTER_CONSUMER_KEY.trim().length() == 0 || TWITTER_CONSUMER_SECRET.trim().length() == 0)
// Internet Connection is not present
alert.showAlertDialog(MainActivity.this, "Twitter oAuth tokens", "Please set your twitter oauth tokens first!", false);
// stop executing code by return
return;
// All UI elements
btnLoginTwitter = (Button) findViewById(R.id.btnLoginTwitter);
btnUpdateStatus = (Button) findViewById(R.id.btnUpdateStatus);
btnLogoutTwitter = (Button) findViewById(R.id.btnLogoutTwitter);
txtUpdate = (EditText) findViewById(R.id.txtUpdateStatus);
lblUpdate = (TextView) findViewById(R.id.lblUpdate);
lblUserName = (TextView) findViewById(R.id.lblUserName);
// Shared Preferences
mSharedPreferences = getApplicationContext().getSharedPreferences("MyPref", 0);
/**
* Twitter login button click event will call loginToTwitter() function
* */
btnLoginTwitter.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View arg0)
// Call login twitter function
loginToTwitter();
);
/**
* Button click event to Update Status, will call updateTwitterStatus()
* function
* */
btnUpdateStatus.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// Call update status function
// Get the status from EditText
String status = txtUpdate.getText().toString();
// Check for blank text
if (status.trim().length() > 0)
// update status
new updateTwitterStatus().execute(status);
else
// EditText is empty
Toast.makeText(
getApplicationContext(),
"Please enter status message",
Toast.LENGTH_SHORT
).show();
);
/**
* Button click event for logout from twitter
* */
btnLogoutTwitter.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View arg0)
// Call logout twitter function
logoutFromTwitter();
);
/** This if conditions is tested once is
* redirected from twitter page. Parse the uri to get oAuth
* Verifier
* */
if (!isTwitterLoggedInAlready())
Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL))
// oAuth verifier
String verifier = uri.getQueryParameter(URL_TWITTER_OAUTH_VERIFIER);
new OAuthAccessTokenTask().execute(verifier);
private class OAuthAccessTokenTask extends AsyncTask<String, Void, Exception>
@Override
protected Exception doInBackground(String... params)
Exception toReturn = null;
try
accessToken = twitter.getOAuthAccessToken(requestToken, params[0]);
user = twitter.showUser(accessToken.getUserId());
catch(TwitterException e)
Log.e(MainActivity.class.getName(), "TwitterError: " + e.getErrorMessage());
toReturn = e;
catch(Exception e)
Log.e(MainActivity.class.getName(), "Error: " + e.getMessage());
toReturn = e;
return toReturn;
@Override
protected void onPostExecute(Exception exception)
onRequestTokenRetrieved(exception);
private void onRequestTokenRetrieved(Exception result)
if (result != null)
Toast.makeText(
this,
result.getMessage(),
Toast.LENGTH_LONG
).show();
else
try
// Shared Preferences
Editor editor = mSharedPreferences.edit();
// After getting access token, access token secret
// store them in application preferences
editor.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken());
editor.putString(PREF_KEY_OAUTH_SECRET,
accessToken.getTokenSecret());
// Store login status - true
editor.putBoolean(PREF_KEY_TWITTER_LOGIN, true);
editor.commit(); // save changes
Log.e("Twitter OAuth Token", "> " + accessToken.getToken());
// Hide login button
btnLoginTwitter.setVisibility(View.GONE);
// Show Update Twitter
lblUpdate.setVisibility(View.VISIBLE);
txtUpdate.setVisibility(View.VISIBLE);
btnUpdateStatus.setVisibility(View.VISIBLE);
btnLogoutTwitter.setVisibility(View.VISIBLE);
// Getting user details from twitter
String username = user.getName();
// Displaying in xml ui
lblUserName.setText(Html.fromHtml("<b>Welcome " + username + "</b>"));
catch (Exception ex)
// Check log for login errors
Log.e("Twitter Login Error", "> " + ex.getMessage());
ex.printStackTrace();
/**
* Function to login twitter
* */
private void loginToTwitter()
// Check if already logged in
if (!isTwitterLoggedInAlready())
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
Configuration configuration = builder.build();
TwitterFactory factory = new TwitterFactory(configuration);
twitter = factory.getInstance();
Thread thread = new Thread(new Runnable()
@Override
public void run()
try
requestToken = twitter.getOAuthRequestToken(TWITTER_CALLBACK_URL);
MainActivity.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthenticationURL())));
catch (Exception e)
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Already Logged into twitter", Toast.LENGTH_LONG).show();
);
thread.start();
else
// user already logged into twitter
Toast.makeText(getApplicationContext(), "Already Logged into twitter", Toast.LENGTH_LONG).show();
/**
* Function to update status
* */
class updateTwitterStatus extends AsyncTask<String, String, String>
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute()
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Updating to twitter...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
/**
* getting Places JSON
* */
protected String doInBackground(String... args)
Log.d("Tweet Text", "> " + args[0]);
String status = args[0];
try
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
// Access Token
String access_token = mSharedPreferences.getString(PREF_KEY_OAUTH_TOKEN, "");
// Access Token Secret
String access_token_secret = mSharedPreferences.getString(PREF_KEY_OAUTH_SECRET, "");
AccessToken accessToken = new AccessToken(access_token, access_token_secret);
Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);
// Update status
twitter4j.Status response = twitter.updateStatus(status);
Log.d("Status", "> " + response.getText());
catch (TwitterException e)
// Error in updating status
Log.d("Twitter Update Error", e.getMessage());
e.printStackTrace();
return null;
/**
* After completing background task Dismiss the progress dialog and show
* the data in UI Always use runOnUiThread(new Runnable()) to update UI
* from background thread, otherwise you will get error
* **/
protected void onPostExecute(String file_url)
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable()
@Override
public void run()
Toast.makeText(getApplicationContext(),
"Status tweeted successfully", Toast.LENGTH_SHORT)
.show();
// Clearing EditText field
txtUpdate.setText("");
);
/**
* Function to logout from twitter
* It will just clear the application shared preferences
* */
private void logoutFromTwitter()
// Clear the shared preferences
Editor e = mSharedPreferences.edit();
e.remove(PREF_KEY_OAUTH_TOKEN);
e.remove(PREF_KEY_OAUTH_SECRET);
e.remove(PREF_KEY_TWITTER_LOGIN);
e.commit();
// After this take the appropriate action
// I am showing the hiding/showing buttons again
// You might not needed this code
btnLogoutTwitter.setVisibility(View.GONE);
btnUpdateStatus.setVisibility(View.GONE);
txtUpdate.setVisibility(View.GONE);
lblUpdate.setVisibility(View.GONE);
lblUserName.setText("");
lblUserName.setVisibility(View.GONE);
btnLoginTwitter.setVisibility(View.VISIBLE);
/**
* Check user already logged in your application using twitter Login flag is
* fetched from Shared Preferences
* */
private boolean isTwitterLoggedInAlready()
// return twitter login status from Shared Preferences
return mSharedPreferences.getBoolean(PREF_KEY_TWITTER_LOGIN, false);
protected void onResume()
super.onResume();
【讨论】:
你的 Twitter Dev 应用设置中的 Twitter 回调 url 必须是 http:// 或 https:// 格式,即使在运行时你的 android 应用会提供它自己的 oauth://t4jsample 值跨度> @daffycricket 您是否面临与***.com/questions/24204855/… 相同的问题?【参考方案4】:这是我的代码中的工作示例,我使用的是 twitter4j,而且你不需要在清单中设置任何意图,因为我使用 webview 而不是浏览器。
放置你的消费者和密钥,你应该很高兴
package com.example.mysituationtwittertest;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity
// Constants
/**
* Register your here app https://dev.twitter.com/apps/new and get your
* consumer key and secret
* */
static String TWITTER_CONSUMER_KEY = "XXXXXXXXXXXXXXXXXXX"; // place your
// cosumer
// key here
static String TWITTER_CONSUMER_SECRET = "XXXXXXXXXXXXXXXX"; // place
// your
// consumer
// secret
// here
// Preference Constants
static String PREFERENCE_NAME = "twitter_oauth";
static final String PREF_KEY_OAUTH_TOKEN = "oauth_token";
static final String PREF_KEY_OAUTH_SECRET = "oauth_token_secret";
static final String PREF_KEY_TWITTER_LOGIN = "isTwitterLogedIn";
static final String TWITTER_CALLBACK_URL = "oauth://youdare";
// Twitter oauth urls
static final String URL_TWITTER_AUTH = "auth_url";
static final String URL_TWITTER_OAUTH_VERIFIER = "oauth_verifier";
static final String URL_TWITTER_OAUTH_TOKEN = "oauth_token";
// Login button
Button btnShareTwitter;
WebView myWebView;
// Twitter
private static Twitter twitter;
private static RequestToken requestToken;
private AccessToken accessToken;
// Shared Preferences
private static SharedPreferences mSharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// All UI elements
btnShareTwitter = (Button) findViewById(R.id.btnShareTwitter);
myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new WebViewClient()
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url)
if (url != null && url.startsWith(TWITTER_CALLBACK_URL))
new AfterLoginTask().execute(url);
else
webView.loadUrl(url);
return true;
);
// Shared Preferences
mSharedPreferences = getApplicationContext().getSharedPreferences(
"MyPref", 0);
/**
* Twitter login button click event will call loginToTwitter() function
* */
btnShareTwitter.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View arg0)
// Call login twitter function
new LoginTask().execute();
);
/**
* Function to login twitter
* */
private void loginToTwitter()
// Check if already logged in
if (!isTwitterLoggedInAlready())
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
Configuration configuration = builder.build();
TwitterFactory factory = new TwitterFactory(configuration);
twitter = factory.getInstance();
try
requestToken = twitter
.getOAuthRequestToken(TWITTER_CALLBACK_URL);
catch (TwitterException e)
// TODO Auto-generated catch block
e.printStackTrace();
else
// user already logged into twitter
Toast.makeText(getApplicationContext(),
"Already Logged into twitter", Toast.LENGTH_LONG).show();
/**
* Check user already logged in your application using twitter Login flag is
* fetched from Shared Preferences
* */
private boolean isTwitterLoggedInAlready()
// return twitter login status from Shared Preferences
return mSharedPreferences.getBoolean(PREF_KEY_TWITTER_LOGIN, false);
public void handleTwitterCallback(String url)
Uri uri = Uri.parse(url);
// oAuth verifier
final String verifier = uri
.getQueryParameter(URL_TWITTER_OAUTH_VERIFIER);
try
// Get the access token
MainActivity.this.accessToken = twitter.getOAuthAccessToken(
requestToken, verifier);
// Shared Preferences
Editor e = mSharedPreferences.edit();
// After getting access token, access token secret
// store them in application preferences
e.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken());
e.putString(PREF_KEY_OAUTH_SECRET, accessToken.getTokenSecret());
// Store login status - true
e.putBoolean(PREF_KEY_TWITTER_LOGIN, true);
e.commit(); // save changes
Log.e("Twitter OAuth Token", "> " + accessToken.getToken());
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
// Access Token
String access_token = mSharedPreferences.getString(
PREF_KEY_OAUTH_TOKEN, "");
// Access Token Secret
String access_token_secret = mSharedPreferences.getString(
PREF_KEY_OAUTH_SECRET, "");
AccessToken accessToken = new AccessToken(access_token,
access_token_secret);
Twitter twitter = new TwitterFactory(builder.build())
.getInstance(accessToken);
// Update status
twitter4j.Status response = twitter
.updateStatus("XXXXXXXXXXXXXXXXX");
catch (Exception e)
e.printStackTrace();
class LoginTask extends AsyncTask<Void, Void, Boolean>
@Override
protected Boolean doInBackground(Void... params)
// TODO Auto-generated method stub
loginToTwitter();
return true;
@Override
protected void onPostExecute(Boolean result)
// TODO Auto-generated method stub
myWebView.loadUrl(requestToken.getAuthenticationURL());
myWebView.setVisibility(View.VISIBLE);
myWebView.requestFocus(View.FOCUS_DOWN);
class AfterLoginTask extends AsyncTask<String, Void, Boolean>
@Override
protected void onPreExecute()
// TODO Auto-generated method stub
myWebView.clearHistory();
@Override
protected Boolean doInBackground(String... params)
// TODO Auto-generated method stub
handleTwitterCallback(params[0]);
return true;
@Override
protected void onPostExecute(Boolean result)
// TODO Auto-generated method stub
myWebView.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "Tweet Successful",
Toast.LENGTH_SHORT).show();
@Override
public void onBackPressed()
if (myWebView.getVisibility() == View.VISIBLE)
if (myWebView.canGoBack())
myWebView.goBack();
return;
else
myWebView.setVisibility(View.GONE);
return;
super.onBackPressed();
【讨论】:
您是否面临与***.com/questions/24204855/…相同的问题? 不,我没有遇到过这个问题,登录后我关闭了我的 webview,所以这个流程不会有这个问题。【参考方案5】:你见过sign-in-with-twittergithub项目吗,它基于twitter4j,实现了Android的Twitter登录。
【讨论】:
以上是关于使用 oauth 和 twitter4j 集成 Android Twitter的主要内容,如果未能解决你的问题,请参考以下文章
Twitter4J + Android:身份验证挑战为空异常
如何将 spring security 与 rest oauth2 服务和 spring social 集成?
Twitter4j 为 getCreatedAt 和 getCountry 变为空白或 null