Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Invalid authorization header format for api requests with volley

Hi,

 

 I am trying to make an api request with volley for an app and have not been able to find much support online. It needs to get the intraday heart rate. I previously had it working without any problems, however it is now giving a 401 error of:

{"errors":[{"errorType":"invalid_client","message":"Invalid authorization header format. Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process."}],"success":false} 

 I have checked, and the token is fine, the app is set to personal, and anything I can think of that wouldn't show as an error has been checked. I am also slightly confused by the error itself, as the docs only show that error under the access token request errors when it is meant to be an api request (that could well be the problem, but again, i cant see where the problem actually is). My code is (error from the button starting the function to when it finishes below):

public String HttpRequestToFitbit() {
RequestQueue queue = Volley.newRequestQueue(context);

final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
final String accessToken = preferences.getString("ACCESS TOKEN", "");

Toast.makeText(context, accessToken, Toast.LENGTH_LONG).show();

//5QSYCS
String baseUrl = "https://api.fitbit.com/1/user/UserID/activities/heart/date/2017-10-23/1d.json";

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, baseUrl, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
String shortResponse = response.toString();
System.out.println(shortResponse);

SharedPreferences.Editor editor = preferences.edit();
editor.putString("RAW RESPONSE", "" + shortResponse);
editor.apply();

}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}
) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Authorization:", "Bearer " + accessToken);
return params;
}
};

queue.add(request);

String response = preferences.getString("RAW RESPONSE", null);
return response;
}

I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
D/libc-netbsd: [getaddrinfo]: hostname=xxxxx; servname=(null); cache_mode=(null), netid=0; mark=0
D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=xxxxx; ai_flags=4; ai_family=0
E/Volley: [27656] BasicNetwork.performRequest: Unexpected response code 401 for https://api.fitbit.com/1/user/5QSYCS/activities/heart/date/2017-10-23/1d.json
E/Volley: [27656] BasicNetwork.performRequest: Unexpected response code 401 for https://api.fitbit.com/1/user/5QSYCS/activities/heart/date/2017-10-23/1d.json
W/System.err: com.android.volley.AuthFailureError
W/System.err: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:159)
W/System.err: at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

I am hoping someone here can point me in the right direction, everything i have is passing the correct values, and it is nothing like the token not being valid.

 

Thanks in advance

-Edmund Carrington

 

Best Answer
0 Votes
0 REPLIES 0