2014-12-03 85 views
0

我一直在尝试将下面的Curl脚本转换为Java以便获取acces令牌,但似乎无论是400还是401都一直失败。我做错了什么? curl是否正确转换为java?请指点...对Java的Crittercism卷曲脚本HttpUrlConnection

卷曲脚本:

其中, “WV3v7ZTbYmqtUOMNvO7oPhLi8RN9zFoo” 是OAuth的客户端ID。

Java代码:

String url = "https://developers.crittercism.com/v1.0/token"; 
    String urlParameters = "grant_type=password&[email protected]&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 

回答

0

它现在正在工作!发现在客户端ID之后不需要冒号,删除它使其工作。但我敢打赌,真正实现它的工作原理可能是我添加的额外请求标题属性。 无论如何,感谢您的时间人!

0

encoded字符串应该包含base64编码CLIENT_ID以及客户端密码,如:

String encoded = new String(Base64.encodeBase64(new String(clientID + ":" + clientPassword))); 
1

david @ crittercis m.com在这里,我写了你正在阅读的文档,并很乐意为此提供帮助。我花了很多心思从API返回描述性错误消息,如果您可以发布这些消息,这将非常有帮助。

有几件事情要检查:

  • WV3v7ZTbYmqtUOMNvO7oPhLi8RN9zFoo是不是在我们的系统中的有效OAuth2用户端编号(我只是检查)。确保您获得适用于您的应用的OAuth2客户端ID。目前我们没有一个自动化的过程(你必须发邮件给支持),但是它在我们的路线图上。
  • 在cURL命令中,-u表示“基本授权”;你有这个权利。我们目前仅支持公共客户端(不保密),这意味着您不需要在我们的OAuth2客户端ID中使用密码。但是,由于您正在手动构建字符串,因此标准规定,即使没有密码,您也必须在用户名和密码之间插入冒号。因此,对于客户端ID WV3v7ZTbYmqtUOMNvO7oPhLi8RN9zFoo,最终的预编码字符串将是“WV3v7ZTbYmqtUOMNvO7oPhLi8RN9zFoo:”,其将编码为“V1YzdjdaVGJZbXF0VU9NTnZPN29QaExpOFJOOXpGb286”(如Hans所述)。

让我知道这些工作是否其中之一。

+0

嘿大卫,谢谢你的回复! 我有一个有效的oAuth ID,通过电子邮件支持收到。 即使进行了您提到的更改,也没有进展。检查上面编辑的代码。 还在OP中添加了抛出的错误异常。 仅供参考,curl脚本在cmd提示符下成功执行,并返回访问令牌。 – Ram 2014-12-04 02:58:21