为了获得访问令牌,我一直试图将下面的Curl脚本转换为Java,但是无论是400还是401,似乎总是失败。我做错了什么?下面是否正确地完成了curl到java的转换?请指点..。
Curl脚本:
curl -X POST https://developers.crittercism.com/v1.0/token -u WV3v7ZTbYmqtUOMNvO7oPhLi8RN9zFoo -d 'grant_type=password&username=david@crittercism.com&password=riTiBw28%3DpmFu'其中,"WV3v7ZTbYmqtUOMNvO7oPhLi8RN9zFoo“是oAuth ClientID。
Java代码:
String url = "https://developers.crittercism.com/v1.0/token";
String urlParameters = "grant_type=password&username=myemail@email.com&password=secretpass";
String clientId = "nD8FiwFiOB1rpU5HVc2ESyRAwbFOOO:"; //Just for ref
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
//add request header
String basicAuth = new String(Base64.encode(clientId.getBytes()));
con.setRequestProperty ("Authorization", String.format("Basic %s", basicAuth));
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Accept", "*/*");
con.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length));
con.setRequestMethod("POST");
// Send post request
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();正在抛出的异常,
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: https://developers.crittercism.com/v1.0/token
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at CrittercismClient.sendPost(CrittercismClient.java:102)
at CrittercismClient.main(CrittercismClient.java:26)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://developers.crittercism.com/v1.0/token
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
at CrittercismClient.sendPost(CrittercismClient.java:96)
... 1 more发布于 2014-12-04 03:05:35
现在起作用了!!发现在clientID之后不需要冒号,删除它就可以工作了。但我敢打赌,真正起作用的可能是我添加的额外请求头属性。不管怎样,谢谢你们的时间,朋友们!
发布于 2014-12-03 23:04:06
david@crittercism.com在这里,我写了你正在读的文档,我很乐意帮你完成这个工作。我非常小心地从API返回描述性错误消息,如果您能够发布这些信息,这将是非常有用的。
有几件事要检查:
让我知道这些作品中的任何一种。
发布于 2014-12-03 08:29:36
encoded字符串应该包含base64编码的client_id以及客户端密码,如下所示:
String encoded = new String(Base64.encodeBase64(new String(clientID + ":" + clientPassword)));https://stackoverflow.com/questions/27266538
复制相似问题