java - How to make authenticated django rest api call from android client? -


i making android app has django rest api backend , want make authenticated network calls using token given user when he/she has logged in.

i using retrofit make requests backend. here doing right attempt make authenticated network call rest api.

@override public void loadallusers() {     call<list<user>> call = userserviceapi.loadallusers();     call.enqueue(new callback<list<user>>() {         @override         public void onresponse(@nonnull call<list<user>> call, @nonnull response<list<user>> response) {             if (response.issuccessful()) {                 list<user> users = response.body();                 eventbus.post(new loadallusersevent(users));             } else {                 eventbus.post(new failloadallusersevent());                 log.d(tag, response.message());             }         }          @override         public void onfailure(@nonnull call<list<user>> call, @nonnull throwable t) {             eventbus.post(new loadusersunreachableserverevent());             log.d(tag, t.tostring());         }     }); } 

here retrofit interface relevant api request:

@get("users/") call<list<user>> loadallusers(@header("authorization: token ") token token); 

when make call passing user's token in header, status code 401: unauthenticated: "get /users/ http/1.1" 401 58

what doing wrong django rest token authentication work , make authenticated django rest api call?

the quick fix change api interface:

@get("users/") call<list<user>> loadallusers(@header("authorization") token token); 

value passing in should formated "token %s".

this not solution, because you'd have pass token around of api calls.

better way solve authorization issues using okhttp client , implement authenticator, takes care of you.

okhttp , retrofit work well.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -