Pergunta

I am trying to start a simple session with grooveshark and am using the sendPostReq function to call the startSession api. I keep on getting the following response from grooveshark.

{"errors":[{"code":2,"message":"Method not found."}]}

The way we work with the grooveshark api is we have the payload(grooveSharkjson in my case), we produce a md5 hash of that using the secret key and post that json to this url https://api.grooveshark.com/ws3.php?sig={md5-hash-of-payload}. Is that the correct procedure?

The sendPostReq function and the code for producing the md5 hash is also present below

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 ;
}

I believe the hash I am producing is correct as I have verified it with the sample key and secret they have supplied us with on their website http://developers.grooveshark.com/tuts/public_api

Foi útil?

Solução

I know I posted the question about 20 mins back, but I just found the solution. There was a problem with the json string, especially the way I was generating it. This is how it should be generated

    String grooveSharkjson = "{\"method\":\"startSession\",\"header\":{\"wsKey\":\"wsKey\"},\"parameters\":[]}";

I did not expect the solution to be so obvious but this is from where I got an idea of how to solve my problem - I tested my key and secret on their sandbox (http://developers.grooveshark.com/docs/public_api/v3/sandbox.php) and double checked the hmac md5 signature.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top