4

I have been trying to build a LastFM scrobbler in my Android app. For that I need to first get session key of the user, for which I am using the auth.getMobileSession.

LastFm getMobileSession

LastFM Rest Services

But I always get the error:

Invalid parameters - Your request is missing a required parameter

I even tried it in Postman using POST call, sending all the Parameters, but the same issue. Am I missing something?enter image description here

Sucho
  • 321
  • 1
  • 14
  • 1
    Did you find a solution for this? I'm having the same issue. – Knolraap Aug 18 '17 at 15:10
  • 1
    Nope! Tried everything @Knolraap – Sucho Aug 19 '17 at 12:02
  • @Knolraap found the solution, if you still have issues. – lastresort Sep 16 '17 at 14:45
  • @Knolraap How did you solve it? Did you use okhttp like the answer mentioned below? – Sucho Sep 17 '17 at 15:18
  • You don't need to use okhttp3 urgently. You just need something to send your request. Just take care that you're adding all parameters to your request, that the request is post, uses https and that your api_sig is the alphabetical combination of your parameters + secret in a hex md5 (to get the md5 you could use my code for the hexString) – lastresort Sep 19 '17 at 06:11

3 Answers3

0

I had the same problem and finally found the solution.

I've used okhttp3 library to send the request, so you need to add this dependency to your build.gradle:

compile 'com.squareup.okhttp3:okhttp:3.9.0'

And here's my working code

    String api_key = // your api key
    String api_sig = // your api sig
    String username = // username you want to log in
    String password = // user password

    String apiSignature = "api_key" + api_key + "methodauth.getMobileSessionpassword" + password + "username" + username + api_sig;

    StringBuilder hexString = new StringBuilder();

    try {
        MessageDigest md5Encrypter = MessageDigest.getInstance("MD5");
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest
                .getInstance("MD5");
        digest.update(apiSignature.getBytes("UTF-8"));
        byte messageDigest[] = digest.digest();

        // Create Hex String
        for (byte aMessageDigest : messageDigest) {
            String h = Integer.toHexString(0xFF & aMessageDigest);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }

        String urlParameter = "method=auth.getMobileSession&api_key=" + api_key + "&password=" + password + "&username=" + username + "&api_sig=" + hexString;
        Request request = new Request.Builder()
                .url("https://ws.audioscrobbler.com/2.0/?" + urlParameter).post(RequestBody.create(null, new byte[0])).build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try (ResponseBody responseBody = response.body()) {
                    String test = responseBody.string(); // your .xml with the session id, see https://www.last.fm/api/show/auth.getSession
                    if (!response.isSuccessful())
                        throw new IOException("Unexpected code " + response);
                }
            }
        });
    } catch (Exception ex) {
        System.out.println(ex.toString());
    }
lastresort
  • 508
  • 3
  • 15
0

I was also struggling with this problem and I finally found the cause.

In a HTTP POST request the parameters are sent in the body of the request, as explained in this question: How are parameters sent in an HTTP POST request?

What I was putting in the request body was:

?api_key=xxxxxxxxxx&method=Auth.getMobileSession&password=xxxxxxxxxx&username=HyperQuantum&api_sig=xxxxxxxxxx

Do you see the error?

The question mark at the beginning is not supposed to be there. I made sure to remove it and after that the problem was fixed.

I am using Qt and used QUrl.toEncoded() to get the encoded parameters for the request, but it also generates a question mark which is not helpful in this case.

HyperQuantum
  • 1,532
  • 1
  • 10
  • 12
-1

Looks like if You want to use LastFM API You don't need to care about instruction of getMobileSession() etc.

Without any session creating and just sending request with API_KEY its working without problems.

Esperanz0
  • 1,524
  • 13
  • 35