1

I'm trying to make a GET request in an Android application using the Volley library. This GET request is to verify account credentials using Basic HTTP Authentication. I verified the URL with credentials works in my browser as it returns successful XML. The format is:

http://username:password@myanimelist.net/api/account/verify_credentials.xml

where username and password obviously represent real user credentials. Volley throws this error:

 BasicNetwork.performRequest: Unexpected response code 401 for http://username:password@myanimelist.net/api/account/verify_credentials.xml

Here is my Android code that handles the request:

private static final String HTTP_PRE = "http://";
private static final String VERIFY_CREDENTIALS = "myanimelist.net/api/account/verify_credentials.xml";

public void verifyCredentials(String username, String password) {
    RequestQueue queue = Volley.newRequestQueue(context);
    String url = HTTP_PRE + username + ":" + password + "@" + VERIFY_CREDENTIALS;

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            processResponse(response);
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            // handle error
            Log.d(TAG, "error: " + error.getMessage());
        }
    });

    queue.add(stringRequest);
}

This solution to override the getHeaders() method provided the same result: How does one use Basic Authentication with Volley on Android?.

Here is my implementation of that solution:

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> params = super.getHeaders();
    if (params == null){
        params = new HashMap<>();
    }
    String creds = String.format("%s:%s", username, password);

    params.put("Authorization", creds);

    return params;
}

Which returned this error without the credentials built directly into the URL:

BasicNetwork.performRequest: Unexpected response code 401 for http://myanimelist.net/api/account/verify_credentials.xml

If someone could provide advice, I'd really appreciate it. This is my first time using Basic HTTP Authentication so I could be missing something obvious.

Community
  • 1
  • 1
Jake Moritz
  • 843
  • 2
  • 9
  • 21

2 Answers2

0

I solved this problem following the first answer here: Http Authentication in android using volley library. I had tried something similar and many other solutions but this was the only one that worked.

Community
  • 1
  • 1
Jake Moritz
  • 843
  • 2
  • 9
  • 21
0

Basic Authentication uses BASE64 encoding. You're missing

String creds = String.format("%s:%s", username, password);
creds = Base64.encodeToString(creds.getBytes(), Base64.NO_WRAP);

The Authorization HTTP header requires to indicate the method used (Basic|Digest). At last you headers should look like this:

GET http://username:password@myanimelist.net/api/account/verify_credentials.xml
Accept: text/xml,text/plain
...
Authorization: Basic XXXXXXXXXXXXXX==
Alwin Kesler
  • 1,450
  • 1
  • 20
  • 41