在将文本发布到tumblr时,我得到的消息不是未授权的401

时间:2012-09-28 11:33:27

标签: android authentication authorization tumblr

我正在使用以下代码将文本发布到我的Android应用程序中的tumblr:

public class Snippet {
    HttpPost hpost = new HttpPost("http://api.tumblr.com/v2/blog/" + blogname.getText().toString() + ".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));
    try 
    {
        hpost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    }
    catch (UnsupportedEncodingException e) 
    {
        debug += e.toString();
    }
    consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
    consumer.setTokenWithSecret(token, secret);
    try 
    {
        consumer.sign(hpost);
    } catch (OAuthMessageSignerException e) 
    {
        debug += e.toString();
    } catch (OAuthExpectationFailedException e) 
    {
        debug += e.toString();
    } catch (OAuthCommunicationException e) 
    {
        debug += e.toString();
    }
    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse resp = null;
    try 
    {
        resp = client.execute(hpost);
    } catch (ClientProtocolException e) 
    {
        debug += e.toString();
    } catch (IOException e) 
    {
        debug += e.toString();
    }
    String result = null;
    try {
        result = EntityUtils.toString(resp.getEntity());
    } catch (ParseException e) 
    {
        debug += e.toString();
    } catch (IOException e) 
    {
        debug += e.toString();
    }
}

然后我得到以下结果:

{"meta":{"status":401,"msg":"Not Authorized"},"response":[]}

我错过了什么吗?我有正确的访问令牌和密钥。

3 个答案:

答案 0 :(得分:0)

检查你的标题,正文或博客名称,如果它等于null你的责任将是401

答案 1 :(得分:0)

您首先需要在tumblr上创建一个博客,然后您就可以在该博客上发帖。您需要创建一个博客然后在该博客上发布..

答案 2 :(得分:0)

HttpPost hpost = new HttpPost("http://api.tumblr.com/v2/blog/" + blogname.getText().toString() + ".tumblr.com/post");

.tumblr.com/post替换为.tumblr.com/posts