Android: How to Twitter oAuth Through Account Manager

旧巷老猫 提交于 2019-12-24 02:59:10

问题


In my application, I want to use oAuth protocol during login processes for several sites like Twitter, Facebook and more. I've some issues about Twitter oAuth.

I used those lines to access token and secret datas:

AccountManager am = AccountManager.get(MyActivity.this);
Account[] accounts = am.getAccountsByType("com.twitter.android.auth.login");

if(accounts.length > 0) {
    Account acct = accounts[0];
    am.getAuthToken(acct, "com.twitter.android.oauth.token", null, MyActivity.this, new AccountManagerCallback<Bundle>() {
        public void run(AccountManagerFuture<Bundle> arg0) {
            // Accessing token
        }
    }, null);               
} else {
    Toast.makeText(getApplicationContext(), "There is no defined account", Toast.LENGTH_SHORT).show();
}

So I succeed it. But I'm curious about these questions:

  • Do I need any Twitter application?
  • How will I check these tokens or do I have to check? I mean how will I reach users' information like twitter_id ?

Any information would be great.


回答1:


Do I need any Twitter application?

Yes, you will need a Twitter application for authentications with a Twitter account. For this, go to Twitter Developers to create your application.

How will I check these tokens or do I have to check? I mean how will I reach users' information like twitter_id ?

When you register your Twitter application on the Twitter developer website, Twitter will generate the consumer key and the consumer secret of your app. You can reset them if you want. They are written in a dedicated section for your app. You can access to this section by going to <your Twitter Developers account> (you can authenticate with your own Twitter credentials) > My Applications and clicking on your app.

For the OAuth token and the OAuth secret, Twitter Developers will generate them for the owner of the application when this last is created. For other users, you will have to do an OAuth Authentication Flow to get their (OAuth) token and secret. You can also retrieve the owner OAuth tokens with this process too. At the end of the process, the Twitter API will give you the final user's tokens (OAuth token and OAuth secret), the Twitter id and the screen name (username) of the authenticated user. With these informations, you will be able to get all the informations you want.



来源:https://stackoverflow.com/questions/11560609/android-how-to-twitter-oauth-through-account-manager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!