Authentication Error when using HttpPost with DefaultHttpClient on Android

非 Y 不嫁゛ 提交于 2019-11-29 03:47:50

Authentication error: Unable to respond to any of these challenges: {}

This error message means that the server responded with 401 (Unauthorized) status code but failed to provide a single auth challenge (WWW-Authenticate header) thus making it impossible for HttpClient to automatically recover from the authentication failure.

Most likely application expects some soft of credentials in the HTML form enclosed in the HTTP POST request.

Don't you have to declare the port and protocol? I'm just swagging this code so please don't be upset if it doesn't immediatley compile correctly. Also, I usually supply a UsernamePasswordCredentials to my setCredentials() but I imagine it's the same.

HttpHost host = new HttpHost("www.foo.com", 443, "https");

// assemble your GET or POST

client.getCredentialsProvider().setCredentials(new AuthScope(host.getHostName(), host.getPort()));

HttpResponse response = client.execute(host, [HttpPost or HttpGet]);

More info about setCredentials here.

Here's how I ended up with similar problem:

DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(username, password));

Thanks to Ryan for right direction.

Not specifying a Callback URL for my Twitter App resulted in the same error for me:

Authentication error: Unable to respond to any of these challenges: {oauth=WWW-Authenticate: OAuth realm="https://api.twitter.com"}

Setting a callback URL on Twitter fixed the problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!