在 Android 中使用 Pinboard API 添加帖子时出现 401 未经授权

发布于 10-31 10:09 字数 1309 浏览 4 评论 0原文

嘿大家, 我正在使用 Pinboard API 从我的 Android 应用程序添加帖子。每次我发送带有所需凭据和参数的 GET 请求时,我都会收到 401 未经授权的响应代码。我在 PHP 代码中尝试了相同的 URL,帖子被添加到 Pinboard 中,没有任何错误。 知道我哪里出错了吗?
这是代码:

    private void postToPinboard(){

    String url = "https://.muUsername:[email protected]/v1/posts/add?";
    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(url);

     try {
            // Adding my data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("description","Description data");
            nameValuePairs.add(new BasicNameValuePair("url", "http://somewebsite.com"));

            String paramString = URLEncodedUtils.format(nameValuePairs, "utf-8");
            url +=paramString;

            // Execute HTTP Post Request
            HttpResponse response = client.execute(get);

            Log.v("", "RESPONSE CODE: "+response.getStatusLine());// giving 401 Unauthorized 

        } catch (ClientProtocolException e) {
            // do something
        } catch (IOException e) {
            // do domething
        }
        finish();
}

Hey all,
I am using the Pinboard API for adding a post from my Android App. Each time I send the GET request with the required credentials and arguments, I get the 401 Unauthorized response code. I tried the same URL from a PHP code and the post gets added to Pinboard without any errors.
Any idea where I am going wrong?
Here's the code:

    private void postToPinboard(){

    String url = "https://.muUsername:[email protected]/v1/posts/add?";
    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(url);

     try {
            // Adding my data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("description","Description data");
            nameValuePairs.add(new BasicNameValuePair("url", "http://somewebsite.com"));

            String paramString = URLEncodedUtils.format(nameValuePairs, "utf-8");
            url +=paramString;

            // Execute HTTP Post Request
            HttpResponse response = client.execute(get);

            Log.v("", "RESPONSE CODE: "+response.getStatusLine());// giving 401 Unauthorized 

        } catch (ClientProtocolException e) {
            // do something
        } catch (IOException e) {
            // do domething
        }
        finish();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

违心°2024-11-07 10:09:56

我终于遇到了问题:使用Pinboard API需要Android支持HTTP基本身份验证。所以这就是我让它工作的方法:

    private void postToPinboard(){

        String url ="https://api.pinboard.in/v1/posts/add?";

        DefaultHttpClient client = new DefaultHttpClient();

    String credentials = Base64.encodeBytes((username+":"+password).getBytes());

    if(credentials!=null){

        try {

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

            nameValuePairs.add(new BasicNameValuePair("description", "Description data");
            nameValuePairs.add(new BasicNameValuePair("url", "http://somewebsite.com"));

            String paramString = URLEncodedUtils.format(nameValuePairs, "utf-8");

            url +=paramString;

            HttpGet get = new HttpGet(url);
            get.addHeader("Authorization","Basic "+credentials);

           HttpResponse response = client.execute(get);

               if(response.getStatusLine().getStatusCode() == 200){
                   //       Added to pinboard
               }else{
                   //       Error adding to Pinboard
               }

        } catch (ClientProtocolException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
finish();

}

I finally got the problem: using Pinboard API requires Android to support HTTP basic authentication. So here's how I got it to work:

    private void postToPinboard(){

        String url ="https://api.pinboard.in/v1/posts/add?";

        DefaultHttpClient client = new DefaultHttpClient();

    String credentials = Base64.encodeBytes((username+":"+password).getBytes());

    if(credentials!=null){

        try {

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

            nameValuePairs.add(new BasicNameValuePair("description", "Description data");
            nameValuePairs.add(new BasicNameValuePair("url", "http://somewebsite.com"));

            String paramString = URLEncodedUtils.format(nameValuePairs, "utf-8");

            url +=paramString;

            HttpGet get = new HttpGet(url);
            get.addHeader("Authorization","Basic "+credentials);

           HttpResponse response = client.execute(get);

               if(response.getStatusLine().getStatusCode() == 200){
                   //       Added to pinboard
               }else{
                   //       Error adding to Pinboard
               }

        } catch (ClientProtocolException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
finish();

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文