如何Twitter的OAuth的在Android的工作?工作、Twitter、OAuth、Android

2023-09-05 06:17:35 作者:难过要藏起来

我一直在寻找了一段时间的答案,我承认我是pretty的坚持。我试图在我的应用程序中实现Twitter和发现它有点挑战性。它似乎做到这一点是与Twitter注册我的应用程序(我已经这样做了)作为一个浏览器应用程序的方式。我不知道,虽然用什么我的回调URL。我真的只想回到我的应用程序中的当前活动,而不是被重定向到一个回调URL。我应该怎么放是什么?我是pretty的坚持......

I have been searching for an answer for quite some time, and I have admit that I am pretty stuck. I am attempting to implement Twitter in my application and have found it somewhat challenging. It seems the way to do it is to register my app with Twitter(I have already done so) as a Browser application. I don't know what to use for my callback url though. I really want to just return to my current activity in the application, and not be redirected to a callback url. What should I put for that? I am pretty stuck...

推荐答案

这是如何工作的?你通话到Twitter认证URL(通过打开Web浏览器)。认证的URL必须包含回调URL。一个回调的URL通常是这样的: X-您的应用程序名称,支持OAuth的Twitter://回调(*)

This is how it works... you do the call to twitter authentication URL (by opening a web browser). The URL of the authentication must contain the callback URL. A callback URL usually looks like this: x-your-application-name-oauth-twitter://callback (*).

第二步是添加一个意图过滤器到你的活动(实施微博身份验证需要你知道如何机器人工程(除非你找到一个教程,做一切对你来说,但我认为并非如此看来你似乎是个聪明的家伙,不是吗?))。不管结果如何,你加入这样的事情在你的清单这样做的:

Second step is to add an intent filter to your Activity (implementing twitter auth requires you to know how Android works (unless you find a tutorial that does everything for you, but I think that's not the case seems you seem to be a smart guy, aren't you?)). Whatever, you do so by adding something like this in your manifest:

<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:scheme="x-your-application-name-oauth-twitter" android:host="callback"/>
</intent-filter>

这基本上是一个方式说给Android操作系统的嘿,伙计,我能应付,看起来像 X-您的应用程序名称,支持OAuth的Twitter任何网址://回调 的。这样一来,一旦用户通过验证,Twitter将调用该URL和您的应用程序将收回控制权。

This is basically a way to say to Android OS: "hey dude, I can handle any URL that looks like x-your-application-name-oauth-twitter://callback". That way, once user has authenticated, twitter will call that URL and your application will reclaim the control.

通常情况下,你的活动必须与 Android的创建:launchMode =singleTask标记,那么您必须重写 onNewIntent 您的活动的方法,将被调用一次你的应用中控制了。在意图您可以找到有关回调信息。

Usually, your activity must be created with the android:launchMode="singleTask" tag, then you must override the onNewIntent method of your activity that will be called once your application has the control again. Inside the Intent you will find information about the callback.

(*)建立,必须启动URL(这将允许用户进行身份验证)是有点困难的。 OAuth是一个很好的,但那种刻苦学习的标准。所以,你可以使用第三方库,这将有助于你在这。你可以,例如,使用OAuth路标Java库。不过,我建议你坚持twitter4j库,这将有助于你与OAuth的,也可以让你与Twitter的API进行交互。

(*) Building the URL that you must launch (and that will allow users to authenticate) is somehow difficult. OAuth is a good but kind of hard to learn standard. So, you can use third-party libraries that will help you on this. You could, for instance, use Oauth Signpost java library. However, I would recommend you to stick to twitter4j library that will help you with OAuth and also allows you to interact with Twitter API.