在 Android 中使用 Pinboard API 添加帖子时出现 401 未经授权
嘿大家, 我正在使用 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();
}
我终于遇到了问题:使用Pinboard API需要Android支持HTTP基本身份验证。所以这就是我让它工作的方法:
}
I finally got the problem: using Pinboard API requires Android to support HTTP basic authentication. So here's how I got it to work:
}