安卓的tumblr支持OAuth指路牌401 [英] Android Tumblr Oauth-signpost 401

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

问题描述

好了,所以,我想提出一个客户端的tumblr为Android,我一直在努力,并未能得到现在的OAuth工作一个星期左右。下面是它去:

Okay, so, I am making a Tumblr client for Android, I've been trying and failing to get OAuth working for about a week now. Here's how its going:

用户会启动这个应用程序。主要活动的onCreate做到这一点:

User fires up the app. Main activity's onCreate does this:

settings = getSharedPreferences(PREFS_NAME, 0);
authToken=settings.getString("OauthToken", "none");
authTokenSecret=settings.getString("OauthSecret", "none");
if(authToken=="none" || authTokenSecret=="none"){
    Intent i = new Intent(getApplicationContext(),Authentication.class);
    startActivity(i);
}

这将启动其中包含的WebView的认证活动。这项活动成功地获得请求令牌,并发送的WebView到的tumblr登录屏幕。用户被要求以允许应用程序访问他们的数据,他们preSS允许,我WebViewClient捕捉回调URL,以及这是否与它:

this launches an authentication activity which contains a WebView. That activity successfully gets the request token, and sends the WebView to the Tumblr login screen. The user is asked to allow access to their data by the app, they press Allow, and my WebViewClient catches the Callback Url, and does this with it:

String[] token = helper.getVerifier(url);
            if (token != null) {
                try {
                    String accessToken[] = helper.getAccessToken(token[1]);
                    editor.putString("OauthToken", accessToken[0]);
                    editor.putString("OauthSecret", accessToken[1]);
                    editor.commit();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            finish();

该辅助类的getAccessToken和getVerifier是这样的:

The helper class's getAccessToken and getVerifier look like this:

public String[] getVerifier(String myUrl) {
    // extract the token if it exists
    Uri uri = Uri.parse(myUrl);
    if (uri == null) {
        return null;
    }

    String token = uri.getQueryParameter("oauth_token");
    String verifier = uri.getQueryParameter("oauth_verifier");
    return new String[] { token, verifier };
}

public String[] getAccessToken(final String verifier)
        throws OAuthMessageSignerException, OAuthNotAuthorizedException,
        OAuthExpectationFailedException, OAuthCommunicationException {
    new Thread(new Runnable() {
        public void run() {
                try {
                    mProvider.retrieveAccessToken(mConsumer, verifier);
                } catch (OAuthMessageSignerException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (OAuthNotAuthorizedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (OAuthExpectationFailedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (OAuthCommunicationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    }).start();
    return new String[] {
            mConsumer.getToken(), mConsumer.getTokenSecret()
    };
}

后来我终于回到主应用程序屏幕,并设法使我的第一个API调用,以获得用户的仪表盘最新的10个职位:

Then I finally go back to the main application screen and try to make my first API call, to get the most recent ten posts on the user's dashboard:

OAuthConsumer myConsumer = new CommonsHttpOAuthConsumer(MainView.authToken, MainView.authTokenSecret);
            HttpGet request = new HttpGet("http://api.tumblr.com/v2/user/dashboard?limit=10");
            myConsumer.sign(request);
            HttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(request);
            HttpEntity entity = response.getEntity();
            BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }

然而,而不是越来越像我一个很好的JSON响应应该是,我得到这样的:

However, instead of getting a nice JSON response like I ought to be, I am getting this:

10-20 16:36:18.110: D/Result(22817): {"meta":{"status":401,"msg":"Not Authorized"},"response":[]}

那么,我做错什么了?谢谢

So where did I go wrong? Thanks

推荐答案

我已经成功地用的路标库Appache HttpCommons GET / POST / PUT操作;。

I've successfully used signpost library with Appache HttpCommons GET/POST/PUTs.

首先,我推出了web视图进行登录的目的,使用下面的code(<$ C C $> ActivityLogin.java ):

Firstly, I've launched the WebView for login purpose, using the following code (ActivityLogin.java):

private static CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(AppConfig.CONSUMER_KEY, AppConfig.CONSUMER_SECRET);
private static OAuthProvider provider = new DefaultOAuthProvider (AppConfig.requestURL, AppConfig.accessURL, AppConfig.authURL);

private void logMeIn() throws ...{
    String authUrl = provider.retrieveRequestToken(consumer,AppConfig.CALLBACK_URL);
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
}

然后,我用 onNewIntent(意向)接收OAuth的回调从previously推出活动

Then, I've used onNewIntent(Intent) for receiving OAuth callbacks from the previously launched Activity:

AppConfig.java code snipet:

AppConfig.java code snipet:

/** OAuth Authorization URL  */
public static final String authURL = mainOAuthUrl+"/authorize/?theme=android";

/** OAuth Request URL    */
public static final String requestURL = mainOAuthUrl+"/request/token/";

/** OAuth Access URL     */
public static final String accessURL = mainOAuthUrl+"/access/token/";

/** OAuth CALLback URL*/
public static final String CALLBACK_URL = "yourapp://twitt";

ActivityLogin.java code snipet:

ActivityLogin.java code snipet:

protected void onNewIntent(Intent intent) {
    Uri uri = intent.getData();
    if (uri != null && uri.toString().startsWith(AppConfig.CALLBACK_URL)) {  
        String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);  

        provider.retrieveAccessToken(consumer, verifier);
        Data.OAuthAccessKey = consumer.getToken();
        Data.OAuthAccessSecret = consumer.getTokenSecret();
    }

}

然后,我的连接code样子的:

And then, my connection code looks like that:

public HttpResponse sampleOauthConnect(String url) throws ...{

    /** setup some connection params */
    HttpContext context = new BasicHttpContext();

    HttpRequestBase request = new HttpGet(url);

    if (Data.OAuthConsumer == null)
        Data.OAuthConsumer = new CommonsHttpOAuthConsumer(AppConfig.CONSUMER_KEY, AppConfig.CONSUMER_SECRET);

    if (Data.OAuthAccessKey == null || Data.OAuthAccessSecret == null)
        throw new LoginErrorException(LoginErrorException.NOT_LOGGED_IN);

    Data.OAuthConsumer.setTokenWithSecret(Data.OAuthAccessKey, Data.OAuthAccessSecret);

    try {
        Data.OAuthConsumer.sign(request);
    } catch (OAuthMessageSignerException e) {
        throw new LoginErrorException(LoginErrorException.OAUTH_EXCEPTION, e);
    } catch (OAuthExpectationFailedException e) {
        throw new LoginErrorException(LoginErrorException.OAUTH_EXCEPTION, e);
    } catch (OAuthCommunicationException e) {
        throw new LoginErrorException(LoginErrorException.OAUTH_EXCEPTION, e);
    }

    HttpClient client = new DefaultHttpClient();

    /** finally execute this request */
    return client.execute(request, context);
}

我可能已经错过了在复制粘贴的东西,只要告诉我,如果这个解决方案会为你工作。我使用的是新邮政1.2.1.1

I may have missed something during copy-paste, just tell me if this solution will work for you. I'm using singpost 1.2.1.1

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

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