通过 Android 的 AccountManager 类进行 Twitter 身份验证
我正在开发一个 Android 应用程序并希望集成 Twitter。
我的理解是,如果在用户设备上安装了官方 Android Twitter 应用程序,那么我们可以使用帐户管理器进行身份验证 此处..如果未安装,则显示 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 注册应用程序时获得的密钥...但我不明白如何将这些密钥用于官方 Android 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, "EXCEPTION@AUTHTOKEN");
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, "EXCEPTION@AUTHTOKEN");
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(){
}
}
I am developing an android application and want to integrate Twitter.
What I understand is if on user's device, official android Twitter app is installed then we can authenticate using account manager as mentined here.. and if not installed then show twitter login web page.
Is my understanding correct?
Now authenticating using twitter web login page is working fine. But how do I login using account manager?
Using AccountsType as "com.twitter.android.auth.login"
I got token and token secret using account manager from
•com.twitter.android.oauth.token
•com.twitter.android.oauth.token.secret
I am using Twitter4J, and authenticating with my CONSUMER_KEY & CONSUMER_SECRET along with recvd. tokens. but authentication always fails.
CONSUMER_KEY & CONSUMER_SECRET are the keys I got when I registered the app at twitter... but I dont understand how can I use these keys with Official Android Twitter app authentication?
Pls. let me know Thanks
Here is my code
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, "EXCEPTION@AUTHTOKEN");
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, "EXCEPTION@AUTHTOKEN");
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(){
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Android 的 AccountManager 返回的
com.twitter.android.oauth.token
和com.twitter.android.oauth.token.secret
凭据仅使用 Twitter 的官方 Consumer Key 和 Secret 进行身份验证。 AFAIK 它们实际上对第三方开发人员没有用。对于 Twitter,我只想说官方的消费者密钥/秘密对“就在那里”,如果 Twitter 通过应用程序更新更改了它们,那么在没有该应用程序更新的情况下,它们会破坏每个用户的 OAuth。
The
com.twitter.android.oauth.token
andcom.twitter.android.oauth.token.secret
credentials returned by Android's AccountManager only authenticate using Twitter's official Consumer Key and Secret. AFAIK They're not actually useful to third party developers.With respect to Twitter I'll just say the official Consumer Key/Secret pair is "out there", and if Twitter changed them via an app update they'd break OAuth for every user without that app update.
好吧,您使用秘密和消费者密钥来实际获取令牌。通过使用 Android 帐户,您可以从他们那里获取令牌。
因此,一般来说,例如,要发布推文,您只需要一个令牌,正如我所说,您可以从帐户或 twitter4j 获得该令牌。因此,从帐户获取令牌后,您需要将其设置为您的 twitter 4jsdk 令牌并定期使用 api。
希望这是有道理的。
well you use the secret and consumer key to actually get a token. by using the android accounts you get the token from them.
So in general to do a tweet for example you only need a token and as i said you get that one either from the accounts or from twitter4j. so after you get the token from the accounts you need to set it as your twitter 4jsdk token and use the api regularly.
Hope this makes sense.