1

I am currently trying to interface with the Tidal API and i'm having some trouble. Here is my code, I am using the Volley Library:

JSONObject pload = new JSONObject();
        try {
            pload.put("username", username);
            pload.put("password", password);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, ENDPOINT, pload, response->{
            Gson gson = new Gson();
            JSONArray data = response.optJSONArray("data");
            Log.d("RESPONSE", response.toString());
        }, error -> {
            String responseBody = null;
            try {
                responseBody = new String(error.networkResponse.data, "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            Log.d("ERROR", responseBody);
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<>();
                headers.put("X-Tidal-Token", "q--------------------------k");
                headers.put("Content-Type", "application/json");
                return headers;
            }
        };
        mqueue.add(request);

The LogCat:

 D/REQUEST: body: {"username":"b-------@gmail.com","password":"p------"} ___ headers: {X-Tidal-Token=q---------------k, Content-Type=application/json} ___ request.tostring: [ ] https://api.tidalhifi.com/v1/login/username 0xcc20303d NORMAL null
 E/Volley: [309] BasicNetwork.performRequest: Unexpected response code 400 for https://api.tidalhifi.com/v1/login/username
 D/ERROR: {"status":400,"subStatus":1002,"userMessage":"password cannot be blank,username cannot be blank"}

As you can see the payload is not empty so i'm a bit confused. Tidal doesn't have an official API but there are some unofficial wrappers I have been using for reference, here are a few examples of used code:

Javascript:

  request({
    method: 'POST',
    uri: '/login/username?token=kgsOOmYk3zShYrNP',
    headers: {
     'Content-Type': 'application/x-www-form-urlencoded'
    },
    form: {
      username: authInfo.username,
      password: authInfo.password,
    }

Java:

 var url = baseUrl.newBuilder()
            .addPathSegment("login")
            .addPathSegment("username")
            .build();

        var body = new FormBody.Builder()
            .add("username", username)
            .add("password", password)
            .build();

        var req = new Request.Builder()
            .url(url)
            .post(body)
            .header(TOKEN_HEADER, token)
            .build();

Java again:

   HttpResponse<JsonNode> jsonResponse = restHelper.executeRequest(Unirest.post(API_URL + "login/username")
                .header("X-Tidal-Token", "wdgaB1CilGA-S_s2")
                .field("username", username)
                .field("password", password));

If needed I can post the links to all the wrappers and provide a tidal token for testing (It's fairly easy to acquire you just need to sniff a packet from the tidal desktop app) . I've tried overriding getParams() but that didn't work. Any help is appreciated!

barbecu
  • 684
  • 10
  • 28
  • Have u try to change the Content-type header ? In your log you're using `application/json`but in the JS example and the 1st java exemple, the Content-type is `application/x-www-form-urlencoded` – yodamad Apr 30 '20 at 23:46
  • changing the content header didn't work, But it seems that this is a different type of request, i tried a few different things that didn't work, how would I go about writing a request of this format? – barbecu May 01 '20 at 14:55
  • Is it required that you use Volley library ? If not switch to OkHttp and u'll be able to do like your 1st java example : https://square.github.io/okhttp/ – yodamad May 01 '20 at 17:02

1 Answers1

1

You're using the wrong format to send the request, You're using Json when you need a Url encoded format, the link for reference: Send form-urlencoded parameters in post request android volley One Solution: A string request:

final String api = "http://api.url";
final StringRequest stringReq = new StringRequest(Request.Method.POST, api, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Toast.makeText(getApplicationContext(), "Login Successful!", Toast.LENGTH_LONG).show();
  //do other things with the received JSONObject
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_LONG).show();
               }
            }) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> pars = new HashMap<String, String>();
                    pars.put("Content-Type", "application/x-www-form-urlencoded");
                    return pars;
                }

                @Override
                public Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> pars = new HashMap<String, String>();
                    pars.put("Username", "usr");
                    pars.put("Password", "passwd");
                    pars.put("grant_type", "password");
                    return pars;
                }
            };
  //add to the request queue
  requestqueue.AddToRequestQueue(stringReq);
Koby 27
  • 1,049
  • 1
  • 8
  • 17