我想用grooveshark启动一个简单的会话,并使用sendPostReq函数调用startSession api。我继续从grooveshark获得以下回复。Grooveshark api总是返回“Method not found”消息
{"errors":[{"code":2,"message":"Method not found."}]}
我们用的Grooveshark API的工作方式是我们拥有的有效载荷(grooveSharkjson在我的情况),我们生产使用密钥的一个MD5哈希和张贴JSON这个网址https://api.grooveshark.com/ws3.php?sig= {MD5哈希-of-有效载荷}。这是正确的程序吗?
的sendPostReq功能及其制造MD5哈希码也存在以下
public static void sendPostReq() throws Exception{
String grooveSharkjson = "{'method':'startSession','header':{'wsKey':'wskey'}}";
String key = "secret"; // Your api key.
String sig = SecurityHelper.getHmacMD5(grooveSharkjson, key);
URL url = new URL("https://api.grooveshark.com/ws3.php?sig=" + sig);
URLConnection connection = url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
OutputStream os = connection.getOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
pw.write(grooveSharkjson);
pw.close();
InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
String response = sb.toString();
System.out.println(response);
}
public static String getHmacMD5(String payload, String secret) {
String sEncodedString = null;
try {
SecretKeySpec key = new SecretKeySpec((secret).getBytes("UTF-8"), "HmacMD5");
Mac mac = Mac.getInstance("HmacMD5");
mac.init(key);
byte[] bytes = mac.doFinal(payload.getBytes("UTF-8"));
StringBuffer hash = new StringBuffer();
for (int i=0; i<bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
hash.append('0');
}
hash.append(hex);
}
sEncodedString = hash.toString();
}
catch (UnsupportedEncodingException e) {}
catch(InvalidKeyException e){}
catch (NoSuchAlgorithmException e) {}
return sEncodedString ;
}
我相信我生产的散列是正确的,因为我与他们已经为我们提供了样本密钥和密码验证它与在其网站上 http://developers.grooveshark.com/tuts/public_api
您是否设法让它工作?我尝试使用您的代码,并得到“您的Web服务密钥无法访问调用的方法”错误。 :/ –
哦,没关系,我想通了,你没有在你的grooveSharkjson中指定第二个\“wsKey \”应该是我的钥匙和字符串键我应该写我的秘密。菜鸟的错误。不管怎么说,还是要谢谢你! –
您是否包含任何jar文件。对我来说,它给SecurityHelper错误。我是新来的Java。你可以帮我@ user1386101 –