我正在開發一個android應用程序並希望整合Twitter。通過Android的AccountManager類進行Twitter身份驗證
我的理解是,如果在用戶的設備上安裝官方安卓Twitter應用程序,那麼我們可以使用帳戶管理器進行身份驗證here ..如果未安裝,則顯示twitter登錄網頁。
我的理解是否正確?
現在使用twitter web登錄頁進行身份驗證工作正常。但是,如何使用客戶經理登錄?
使用AccountsType爲 「com.twitter.android.auth.login」 我得到令牌和令牌密鑰使用的客戶經理從 •com.twitter.android.oauth.token •com.twitter.android.oauth。 token.secret
我正在使用Twitter4J,並使用我的CONSUMER_KEY & CONSUMER_SECRET和recvd進行身份驗證。令牌。但驗證總是失敗。
CONSUMER_KEY & CONSUMER_SECRET是我在Twitter上註冊應用程序時得到的鑰匙......但我不明白我如何使用官方安卓Twitter應用程序身份驗證中的這些密鑰?
請注意。讓我知道感謝
這裏是我的代碼
public class TwitterAuthentication {
private static final String TAG = "TwitterAuthentication";
private static final int MSG_GOT_AUTH_TOKEN = 100;
private static final int MSG_GOT_AUTH_SECRET = 101;
private static final int MSG_NO_AUT_TOKEN_RECVD = 102;
public static Twitter mTwitter = null;
private Activity mActivity = null;
private SharedPreferences prefs;
private MessageHandler handler = new MessageHandler();
public static boolean bAuthenticationDone = false;
public TwitterAuthentication(Activity activity){
mActivity = activity;
prefs = PreferenceManager.getDefaultSharedPreferences(mActivity);
if (null == mTwitter){
mTwitter = new TwitterFactory().getInstance();;
mTwitter.setOAuthConsumer(Constant.CONSUMER_KEY, Constant.CONSUMER_SECRET);
bAuthenticationDone = false;
}
}
public void LoginTwitter(){
if (Constants.DEBUG)Log.d(TAG,"LoginTwitter");
if (bAuthenticationDone){
TwitterSessionEvents.onLoginSuccess();
}
else if (!isSessionValid()){
AuthTwitter();
}
else{
bAuthenticationDone = true;
TwitterSessionEvents.onLoginSuccess();
}
}
public boolean isSessionValid(){
boolean ret = false;
if (null != prefs && null != mTwitter){
String token = prefs.getString(Constant.OAUTH_TOKEN, "");
String secret = prefs.getString(Constant.OAUTH_TOKEN_SECRET, "");
if (null != token && null != secret && token.length()>0 && secret.length()>0){
AccessToken a = new AccessToken(token,secret);
mTwitter.setOAuthAccessToken(a);
try {
mTwitter.getAccountSettings();
keys.User_Id = mTwitter.getScreenName();
ret = true;
} catch (TwitterException e) {
ret = false;
}
}
}
return ret;
}
public void AuthTwitter(){
// First check if Account manager has valid token
// Result of this is send in MSG
CheckAccManagerForTwitter();
}
public Twitter getTwitter(){
return mTwitter;
}
private boolean CheckAccManagerForTwitter(){
AccountManager am = AccountManager.get(mActivity);
Account[] accts = am.getAccountsByType("com.twitter.android.auth.login");
if(accts.length > 0) {
Account acct = accts[0];
am.getAuthToken(acct, "com.twitter.android.oauth.token", null, mActivity, new AccountManagerCallback<Bundle>() {
@Override
public void run(AccountManagerFuture<Bundle> arg0) {
try {
Bundle b = arg0.getResult();
String token = b.getString(AccountManager.KEY_AUTHTOKEN);
String userName = b.getString(AccountManager.KEY_ACCOUNT_NAME);
handler.sendMessage(handler.obtainMessage(MSG_GOT_AUTH_TOKEN, token));
}
catch (Exception e) {
Log.e(TAG, "[email protected]");
handler.sendEmptyMessage(MSG_NO_AUT_TOKEN_RECVD);
}
}}, null);
am.getAuthToken(acct, "com.twitter.android.oauth.token.secret", null, mActivity, new AccountManagerCallback<Bundle>() {
@Override
public void run(AccountManagerFuture<Bundle> arg0) {
try {
Bundle b = arg0.getResult();
String secret = b.getString(AccountManager.KEY_AUTHTOKEN);
handler.sendMessage(handler.obtainMessage(MSG_GOT_AUTH_SECRET,secret));
}
catch (Exception e) {
Log.e(TAG, "[email protected]");
handler.sendEmptyMessage(MSG_NO_AUT_TOKEN_RECVD);
}
}}, null);
//
}
else{
// No twitter account found in Account Manager
Log.e(TAG, "No Twitter account in Account manager");
handler.sendEmptyMessage(MSG_NO_AUT_TOKEN_RECVD);
}
return true;
}
class MessageHandler extends Handler {
String token = null;
String secret = null;
@Override
public void handleMessage(Message msg) {
if (msg.what == MSG_GOT_AUTH_TOKEN | msg.what ==MSG_GOT_AUTH_SECRET){
if (msg.what == MSG_GOT_AUTH_TOKEN){
token = (String)msg.obj;
}
else if (msg.what == MSG_GOT_AUTH_SECRET){
secret = (String)msg.obj;
}
if (null != token && null != secret){
AccessToken accesstoken = new AccessToken(token,secret);
mTwitter.setOAuthAccessToken(accesstoken);
try {
mTwitter.getAccountSettings();
keys.User_Id = mTwitter.getScreenName();
} catch (Exception e) {
// That means Authentication Failed
// So fall back to web login
Intent i = new Intent(mActivity.getApplicationContext(), PrepareRequestTokenActivity.class);
mActivity.startActivity(i);
}
}
}
else if (msg.what == MSG_NO_AUT_TOKEN_RECVD){
// That means There is no twiter account with Account Manager
// So fall back to web login
Intent i = new Intent(mActivity.getApplicationContext(), PrepareRequestTokenActivity.class);
mActivity.startActivity(i);
}
}
}
public void LogoutTwiter(){
}
}
@DArko感謝您的回覆,我明白你的意思,但問題是當我在Twitter4J中將其設置爲AccessToken時,我發現異常oAuth失敗。 – Naveen 2011-06-15 04:40:01
@DArko我在我的文章中添加了代碼,只是爲了展示我在做什麼,請。讓我知道我錯在哪裏。您是否嘗試記錄您正在接收的令牌的 – Naveen 2011-06-15 04:50:37
?也許嘗試使用來自瀏覽器的令牌,看它是否有效或至少是否正確接收。代碼似乎很好,儘可能多我可以告訴.. – DArkO 2011-06-15 06:07:17