How Does One Use Basic Authentication with Volley on Android

How does one use Basic Authentication with Volley on Android?

Yes it's possible. You need to override Request.getHeaders(). I'm lazy and I used HttpHeaders and HttpAuthentication from Spring for Android but you can just build the auth header and return it from the method. From getHeaders() you can return the auth header for basic auth. This is a sample request with basic auth.

public class GetUser extends Request<User> {

private static final String TAG = GetUser.class.getName();

private Response.Listener<User> mListener;
private ObjectMapper mMapper = new ObjectMapper();

public GetUser(Response.ErrorListener errorListener, Response.Listener<User> listener){
super(Method.GET, PoisUtils.BASE_URL + "/users", errorListener);

mListener = listener;
}

@Override
protected Response<User> parseNetworkResponse(NetworkResponse response) {
String jsonString = new String(response.data);
try {
User result = mMapper.readValue(jsonString, User.class);
return Response.success(result, getCacheEntry());
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
return null;
}

@Override
protected void deliverResponse(User response) {
mListener.onResponse(response);
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return AuthUtils.buildAuthHeaders().toSingleValueMap();
}
}

And here is how I build the auth headers

public static HttpHeaders buildAuthHeaders(){

if(UserUtils.isUserLogged()){
HttpHeaders requestHeaders = new HttpHeaders();
User user = PoisApplication.get().getUser();

HttpAuthentication auth = new HttpBasicAuthentication(
user.getUsername(), user.getPassword());
requestHeaders.setAuthorization(auth);

return requestHeaders;
}
return null;
}

Send authentication information with Volley request

For authentication, IMHO, you should override getHeaders instead of getParams, please try the following:

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
String credentials = "username:password";
String auth = "Basic "
+ Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
headers.put("Content-Type", "application/json");
headers.put("Authorization", auth);
return headers;
}

Hope it helps!

How to send Authorization header in Android using Volley library?

StringRequest request = new StringRequest(Request.Method.POST, YourUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (!response.equals(null)) {
Log.e("Your Array Response", response);
} else {
Log.e("Your Array Response", "Data Null");
}
}

}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("error is ", "" + error);
}
}) {

//This is for Headers If You Needed
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json; charset=UTF-8");
params.put("token", ACCESS_TOKEN);
return params;
}

//Pass Your Parameters here
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("User", UserName);
params.put("Pass", PassWord);
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
queue.add(request);

Basic authentication in android using Volley for GIthub. Getting error code 422

I found what the problem was. I did not think of the possibility that it can be a GET request. I changed the REQUEST.Method.POST to REQUEST.Method.GET and it worked.
Yes, I know. I am still a noob when it comes to stuff related to the web.



Related Topics



Leave a reply



Submit