使用访问令牌和令牌密钥在Android应用中发布到tumblr

时间:2014-12-31 11:34:25

标签: android tumblr

我正在尝试使用访问令牌在tumblr上发布文本。我从Web应用程序的Web服务获取这些访问令牌。请帮忙解决这个问题

这是我的代码,它在执行后显示此消息“身份验证错误:无法响应任何这些挑战”

                String token = list.get(i).getAccess_token();
                String tokenSecret = list.get(i).getAccess_token_secret();

                System.out.println("token :"+token);
                System.out.println("token secret :"+tokenSecret);


                HttpPost hpost = new HttpPost("http://api.tumblr.com/v2/blog/" +list.get(i).getUser_Name()+ ".tumblr.com/post");

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("type", "text"));
                nameValuePairs.add(new BasicNameValuePair("title", title));
                nameValuePairs.add(new BasicNameValuePair("body", body));

                CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(Tumblr.TUMBLR_CONSUMERKEY, Tumblr.TUMBLR_SECRETKEY);
                consumer.setTokenWithSecret(token, tokenSecret);

                try {
                    consumer.sign(hpost);
                } catch (OAuthMessageSignerException 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();
                }

                DefaultHttpClient client = new DefaultHttpClient();
                HttpResponse resp = null;

                try {
                    resp = client.execute(hpost);
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

1 个答案:

答案 0 :(得分:0)

以下代码为我工作:

public class PostToTumblr extends AsyncTask<String, String, String> {
        private String userName;
        ProgressDialog progressDlg;
        int pos;
        CommonsHttpOAuthConsumer oAuthConsumer;
        CommonsHttpOAuthProvider oAuthprovider;
        TumblrUser user;

        public PostToTumblr(TumblrUser user) {
            this.user= user;
            progressDlg = new ProgressDialog(mContext);
            progressDlg.setMessage("Posting Message ");

            this.userName = user.getName();
            oAuthConsumer = new CommonsHttpOAuthConsumer(Const.CONSUMER_KEY,
                    Const.CONSUMER_SECRET);
            oAuthConsumer.setTokenWithSecret(user.getToken(), user.getSecret());

            oAuthprovider = new CommonsHttpOAuthProvider(Const.REQUEST_URL,
                    Const.ACCESS_URL, Const.AUTHORIZE_URL);
            oAuthprovider.setOAuth10a(true);

        }

        protected void onCancelled() {
            progressDlg.cancel();
        }

        protected void onPreExecute() {
            super.onPreExecute();
            progressDlg.show();
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            progressDlg.dismiss();

        }

        @Override
        protected String doInBackground(String... params) {
            try {
                oAuthprovider.retrieveAccessToken(oAuthConsumer, user.getSessionUri().getQueryParameter("oauth_verifier"));
            } catch (OAuthMessageSignerException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (OAuthNotAuthorizedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (OAuthExpectationFailedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (OAuthCommunicationException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            DefaultHttpClient client = new DefaultHttpClient();
            HttpResponse resp = null;
            String result = null;
            HttpPost hpost = new HttpPost("http://api.tumblr.com/v2/blog/"
                    + userName + ".tumblr.com/post");
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("type", "photo"));
            nameValuePairs.add(new BasicNameValuePair("caption",
                    "hello final message for testing"));
            nameValuePairs.add(new BasicNameValuePair("source",
                    "http://www.gstatic.com/webp/gallery/1.jpg"));

            try {
                hpost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                oAuthConsumer.sign(hpost);
                resp = client.execute(hpost);
                result = EntityUtils.toString(resp.getEntity());
                Log.v("result >>", result);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (OAuthMessageSignerException 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();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
    }