2

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

user1386101
  • 1,945
  • 1
  • 14
  • 21

1 Answers1

2

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.

user1386101
  • 1,945
  • 1
  • 14
  • 21
  • Did you manage to get it working? I try to use your code and i get "Your web services key does not have access to the invoked method" error. :/ – Alex Fragotsis Jan 25 '14 at 19:34
  • Oh never mind i figured out, YOu didnt specify that in your grooveSharkjson the second \"wsKey\" should be my key and the String key I should write my secret. Rookie mistake. Thanks anyway! – Alex Fragotsis Jan 25 '14 at 19:56
  • Did u include any jar files. For me it is giving error in SecurityHelper. I am new to java. Can u help me @user1386101 – Priya Sunanthan Feb 17 '14 at 10:45
  • Where is the Class file for SecurityHelper – Priya Sunanthan Feb 17 '14 at 11:00
  • Its a helper class I have made. Here is a link to the file https://github.com/shroffrushabh/jGrooveShark/blob/master/src/com/http/util/SecurityHelper.java – user1386101 Feb 18 '14 at 05:25