如何从我的 android 应用程序使用 Oauth 登录 twitter? [英] How to Login twitter using Oauth from my android application?

查看:46
本文介绍了如何从我的 android 应用程序使用 Oauth 登录 twitter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,它是一个 Twitter 客户端.这意味着它使用 OAuth 连接到 Twitter.我将我的应用程序注册到 Twitter 并获得了我的所有密钥,但现在我不知道如何将我的应用程序与 Twitter 连接.我已经做了一些下面提到的代码.请帮帮我..

I am making an application which is a Twitter client. This means it connects to Twitter with OAuth. I register my application to Twitter and got all my keys, but now I do not have an idea how to connect my application with twitter. I have done some code mention below. Please help me out..

Twitter twitter=new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Consumer key, Consumer secret);
RequestToken requestToken = twitter.getOAuthRequestToken();
AccessToken accessToken=null;
Log.i("Acces Token",accessToken.getToken());     
Log.i("Acces Tokensec",accessToken.getTokenSecret());

提前致谢.

推荐答案

您有两个选择.第一更容易.第二个更难.

You have two choices. Number one is easier. Number two is more difficult.

第一个会从您上次中断的地方继续.获得 requestToken 后,您需要启动一个 WebView 并将 URL 指向 requestToken.getAuthorizationURL().然后用户将登录并选择是否允许访问他/她的帐户.接下来,如果他/她点击允许,将显示一个访问代码,用户必须将其复制/粘贴到您自己的应用程序中.您将使用该密钥和 getOAuthAccessToken()(我认为,我使用了稍后描述的困难方法)来获取您应该永久存储在某处的身份验证令牌.此时您已通过身份验证.

Number one just continues where you left off. After you get a requestToken, you will need to launch a WebView and point the URL at requestToken.getAuthorizationURL(). The user will then log in and choose whether or not to allow access to his/her account. Next, if he/she hit allow, an access code will be displayed the user must copy/paste in your your own application. You will use that key with getOAuthAccessToken() (I think, I used the difficult way described later) to get the auth token that you should store somewhere permanently. At this point you are authenticated.

第二个也从你离开的地方继续减去一个细节......twitter.getOAuthRequestToken(REDIRECT_URL).该 redirect_url 必须首先在您的 twitter 开发者帐户中设置.然后按照与第一个相同的步骤进行操作,只是您的 webview 需要自定义.您需要在 WebView 上使用 setWebViewClient() 并创建一个扩展 WebViewClient 的新类.在 WebViewClient 的 onPageStarted 中,检查 URL 是否以您的返回 url 开头.然后获取 oauth 信息:

Number two also continues where you left off minus one detail... twitter.getOAuthRequestToken(REDIRECT_URL). That redirect_url must be set inside your twitter developer account first. Then follow the same steps as number one except that your webview needs to be customized. You need to use setWebViewClient() on your WebView and create a new class that extends WebViewClient. Inside the WebViewClient's onPageStarted, check if the URL starts with your return url. And then get the oauth info:

String oauth_token = uri.getQueryParameter("oauth_token");
String oauth_verifier = uri.getQueryParameter("oauth_verifier");

使用 oauth_verifiertwitter.getOAuthAccessToken() 来获取您的令牌.

Use the oauth_verifier with twitter.getOAuthAccessToken() to get your token.

这篇关于如何从我的 android 应用程序使用 Oauth 登录 twitter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆