安卓的tumblr OAuth的困惑 [英] Android Tumblr oAuth Confusion

查看:243
本文介绍了安卓的tumblr OAuth的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现在Android应用程序中的tumblr API。我真的自己被授权用户,以便他们可以做的事情,如职位,查看他们的仪表板。我真的不明白的OAuth和排序的tumblr API文档的权利在它跳过。我不知道如果我应该提示用户输入他们的凭据,或做什么用的,一旦我有他们,或类似的东西。我加入了路标库到我的项目,但我一直在抓我的头至今。任何人都熟悉的OAuth在Android上谁还会在乎填补我?谢谢!

I'm trying to implement the Tumblr API in an Android app. I'm really getting stuck with authorizing a user so that they can do things such as post and view their dashboard. I don't really understand oAuth, and the Tumblr API documentation sort of skips right over it. I have no idea if I'm supposed to prompt the user for their credentials, or what to do with those once I have them, or anything like that. I added the Signpost library to my project, but I've been scratching my head since then. Anybody familiar with oAuth on Android who would care to fill me in? Thanks!

推荐答案

是的,文件并不好。你应该先了解OAuth的。 微博有一个很好的概述。

Yes, the documentation is not that good. You should first read about OAuth. Twitter has a good overview.

首先你需要一个消费者密钥和密码(可以让那些通过注册在tumblr你的应用程序)。在这之后,你应该使用的tumblr提供了从用户那里得到授权的身份验证网址。通常你会生成一个请求的URL,从中你可以把用户的浏览器,他/她将登录和授权的应用程序。这将触发回调到您的应用程序,你将能够获得OAuth的令牌。将它保存在您的应用程序(共享preferences),这样你就不必要求用户再次进行身份验证。有了这个道理,你就可以用的tumblr的API需要身份验证交互。

First of all you need a consumer key and secret (you can get those by registering your app in tumblr). After that, you should use the auth URLs that Tumblr provides to get the authorization from the user. Usually you will generate a request URL, from which you can take the user to the browser where he/she will login and authorize your app. This will trigger a callback to your app, and you will be able to get the oAuth token. Save this in your app (SharedPreferences) so that you won't need to ask the user again to authenticate. With this token you will be able to interact with Tumblr's API that requires authentication.

请注意,您还可以实现让用户使用浏览器的web视图代替。不过,这需要多一点的工作。

Note that you could also implement a webview instead of making the user use the browser. Though, this requires a bit more of work.

我发现最新的路标库不同的tumblr工作。你需要一个大一点的版本。这里头和下载这些文件:

I have found that the latest signpost library does not work well with Tumblr. You will need a bit older version. Head here and download these files:

  • signpost-core-1.2.jar
  • signpost-commonshttp4-1.2.jar (this is needed specially if you want to target pre-froyo devices)

导入这两个库到您的项目。要使用它们,基本上你需要调用以下code:

Import both libraries to your project. To use them, basically you need to call the following code:

CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY,
         CONSUMER_SECRET);
CommonsHttpOAuthProvider provider = new CommonsHttpOAuthProvider(
         REQUEST_TOKEN_URL,
         ACCESS_TOKEN_URL,
         AUTH_URL);
String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL); 

CALLBACK_URL可能是这样的:tumblrapp://tumblrapp.com/ok。没有必要对的tumblr设置设置回调URL

CALLBACK_URL could be something like this: "tumblrapp://tumblrapp.com/ok". There is no need to set the callback URL on the Tumblr settings.

此外,您将需要设置一个意图过滤器,使您的应用程序得到授权之后调用。确保您的清单如下:

Also, you will need to set an intent filter so your app gets called after authorization. Make sure that your Manifest looks like this:

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <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="tumblrapp"/>
            </intent-filter>

现在的身份验证后,就可以得到令牌是这样的:

Now after authentication you can get the token like this:

Uri uri = this.getIntent().getData();
if (uri != null) {
   String token = uri.getQueryParameter("oauth_token");
}

我做了一个快速的示例应用程序。你可以在这里检查出来。您可能需要请求移动到后台线程,因为它会阻止用户界面。

I made a quick sample app. You can check it out here. You might want to move the request to a background thread as it will block the UI.

这篇关于安卓的tumblr OAuth的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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