从Tumblr获取用户信息

时间:2013-11-28 12:23:51

标签: android networking oauth tumblr

我首先从浏览器中获取令牌和验证程序,如下所示:

CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
                CommonsHttpOAuthProvider provider = new CommonsHttpOAuthProvider(REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, AUTH_URL);
                String authUrl;
                try {
                    authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
                    LogService.log(TAG, "Auth url:" + authUrl);

                    context.startActivity(new Intent("android.intent.action.VIEW", Uri.parse(authUrl)));

                } 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();
                }

之后,回调将我带到另一个类,我尝试获取这样的用户信息:

Uri uri = getIntent().getData();
    if (uri != null) {

        Constants.TumblrToken = uri.getQueryParameter("oauth_token");
        Constants.TumblrVerifier = uri.getQueryParameter("oauth_verifier");
        String user = uri.getQueryParameter("username");

        LogService.log("", "Token:" + Constants.TumblrToken);
        LogService.log("", "Verifier:" + Constants.TumblrVerifier);
        Thread th = new Thread(new Runnable() {

            @Override
            public void run() {
                JumblrClient client = new JumblrClient(Util.CONSUMER_KEY, Util.CONSUMER_SECRET);
                client.setToken(Constants.TumblrToken, Constants.TumblrVerifier);

                User user = client.user();
                LogService.log("", "user.getName(): " + user.getName());

            }
        });
        th.start();
    }

现在这是我回来的错误:

11-28 14:19:49.925: E/AndroidRuntime(30600): FATAL EXCEPTION: Thread-1474
11-28 14:19:49.925: E/AndroidRuntime(30600): org.scribe.exceptions.OAuthConnectionException: There was a problem while creating a connection to the remote service.
11-28 14:19:49.925: E/AndroidRuntime(30600):    at org.scribe.model.Request.send(Request.java:66)
11-28 14:19:49.925: E/AndroidRuntime(30600):    at org.scribe.model.OAuthRequest.send(OAuthRequest.java:12)
11-28 14:19:49.925: E/AndroidRuntime(30600):    at com.tumblr.jumblr.request.RequestBuilder.get(RequestBuilder.java:69)
11-28 14:19:49.925: E/AndroidRuntime(30600):    at com.tumblr.jumblr.JumblrClient.user(JumblrClient.java:64)
11-28 14:19:49.925: E/AndroidRuntime(30600):    at com.imagechef.activities.BackFromTumblr$1.run(BackFromTumblr.java:34)
11-28 14:19:49.925: E/AndroidRuntime(30600):    at java.lang.Thread.run(Thread.java:856)
11-28 14:19:49.925: E/AndroidRuntime(30600): Caused by: java.io.IOException: Received authentication challenge is null
11-28 14:19:49.925: E/AndroidRuntime(30600):    at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:397)
11-28 14:19:49.925: E/AndroidRuntime(30600):    at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:345)
11-28 14:19:49.925: E/AndroidRuntime(30600):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:276)
11-28 14:19:49.925: E/AndroidRuntime(30600):    at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
11-28 14:19:49.925: E/AndroidRuntime(30600):    at org.scribe.model.Response.<init>(Response.java:29)
11-28 14:19:49.925: E/AndroidRuntime(30600):    at org.scribe.model.Request.doSend(Request.java:106)
11-28 14:19:49.925: E/AndroidRuntime(30600):    at org.scribe.model.Request.send(Request.java:62)
11-28 14:19:49.925: E/AndroidRuntime(30600):    ... 5 more

1 个答案:

答案 0 :(得分:0)

您需要在AsyncTask中运行网络操作