android - Implementing Oauth2 with login credentials from native login page -
i trying implement oauth2 based web service. have clientid, clientsecret, authorization endpoint, token endpoint , callback url(custom schema points android native page). when checked other oauth2 based apis, has login url , redirected login web page. in case there not login url, should redirected native login page , on success response should redirected logged in native page. how access token using oauth2? appreciated.
from comments above, let's assume have had asp.net webapi server-side app, , if android client app uses httpurlconnection
, can refer following sample code (of course, need modify more make works requirement):
string address = "http://<ip>:<port>/token"; httpurlconnection urlconnection; string requestbody; uri.builder builder = new uri.builder(); map<string, string> stringmap = new hashmap<>(); stringmap.put("grant_type", "password"); stringmap.put("username", "bnk"); stringmap.put("password", "bnk"); iterator entries = stringmap.entryset().iterator(); while (entries.hasnext()) { map.entry entry = (map.entry) entries.next(); builder.appendqueryparameter(entry.getkey().tostring(), entry.getvalue().tostring()); entries.remove(); } requestbody = builder.build().getencodedquery(); try { url url = new url(address); urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setdoinput(true); urlconnection.setdooutput(true); urlconnection.setrequestmethod("post"); urlconnection.setrequestproperty("content-type", "application/x-www-form-urlencoded"); outputstream outputstream = new bufferedoutputstream(urlconnection.getoutputstream()); bufferedwriter writer = new bufferedwriter(new outputstreamwriter(outputstream, "utf-8")); writer.write(requestbody); writer.flush(); writer.close(); outputstream.close(); urlconnection.connect(); if (urlconnection.getresponsecode() == httpurlconnection.http_ok) { // something... } else { // something... } // something... } catch (exception e) { e.printstacktrace(); }
update:
if prefer okhttp, please refer following working code:
private class accesstokenrequest extends asynctask<void, void, string> { @override protected string doinbackground(void... voids) { string accesstoken = null; okhttpjsonrequest jsonrequest = new okhttpjsonrequest(); requestbody requestbody = new formencodingbuilder() .add("grant_type", "password") .add("username", "bnk") .add("password", "bnk123") .build(); try { jsonobject jsonobject = jsonrequest.post("http://192.168.1.100:24780/token", requestbody); if (!jsonobject.isnull("access_token")) { accesstoken = jsonobject.getstring("access_token"); } } catch (ioexception | jsonexception e) { e.printstacktrace(); } return accesstoken; } @override protected void onpostexecute(string response) { super.onpostexecute(response); // such storing token furture requests } } public class okhttpjsonrequest { okhttpclient client = new okhttpclient(); jsonobject post(string url, requestbody body) throws ioexception, jsonexception { request request = new request.builder() .url(url) .post(body) .build(); response response = client.newcall(request).execute(); return new jsonobject(response.body().string()); } }
hope helps!
Comments
Post a Comment