azure - ADAL4j java - use refresh token with username and password to get the access token -


i connecting azure ad enabled api using java back-end server. able access token following java code.

    string tenantid = "************";     string username = "***************";     string password = "*************";     string clientid = "**********";     string resource = "***********";     string useremail = "**********";       authenticationcontext authcontext = null;     authenticationresult authresult = null;     executorservice service = null;      try     {         service = executors.newfixedthreadpool( 1 );         string url = "https://login.microsoftonline.com/" + tenantid + "/oauth2/authorize";         authcontext = new authenticationcontext( url, false, service );         future<authenticationresult> future = authcontext.acquiretoken(                 resource,                 clientid,                 useremail,                 password,                 null );          authresult = future.get();      }     catch( exception ex )     {         ex.printstacktrace();     } 

please note api provider is not supporting client credentials currently.

the issue me is, using refresh token received in above code new access token.

adal4j java library doesn't seems have methods supporting this. a documentation java library

but in .net library there methods like,

public authenticationresult acquiretokenbyrefreshtoken( string refreshtoken, string clientid, string resource ) 

for refreshing access token without credential provided.

why these methods not provided in java library?. there restrictions? , possible workarounds?

thanks in advance.

as far know, although java adal4j library doesn't support method

public authenticationresult acquiretokenbyrefreshtoken( string refreshtoken, string clientid, string resource ) 

which supported in .net library, both of 2 types of libraries implemented via http rest api.

you refer refreshing access tokens in official document

// line breaks legibility  post /{tenant}/oauth2/token http/1.1 host: https://login.microsoftonline.com content-type: application/x-www-form-urlencoded  client_id=6731de76-14a6-49ae-97bc-6eba6914391e &refresh_token=oaaabaaaail9kn2z27uubvwfpbm0glwqjvzcte9ukp3psx1axxujq... &grant_type=refresh_token &resource=https%3a%2f%2fservice.contoso.com%2f &client_secret=jqqx2pno9bpm0ueihupzyrh    // note: required web apps 

i use postman test request acquiring accesstoken refreshtoken without credentials reference:

enter image description here

corresponded, implemented request following java code

import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.outputstream; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import java.util.concurrent.future;  import com.microsoft.aad.adal4j.authenticationcontext; import com.microsoft.aad.adal4j.authenticationresult;  public class acquiretokenbyrefreshtoken {      static string tenantid = "***";     static string username = "***";     static string password = "***";     static string clientid = "***";     static string resource = "https://graph.windows.net";     static string useremail = "***";      public static void main(string[] args) throws malformedurlexception, ioexception {         authenticationcontext authcontext = null;         authenticationresult authresult = null;         executorservice service = null;          try {             service = executors.newfixedthreadpool(1);             string url = "https://login.microsoftonline.com/" + tenantid + "/oauth2/authorize";             authcontext = new authenticationcontext(url, false, service);             future<authenticationresult> future = authcontext.acquiretoken(resource, clientid, useremail, password,                     null);              authresult = future.get();             system.out.println("get access token: \n" + authresult.getaccesstoken());             system.out.println("get refresh token: \n" + authresult.getrefreshtoken());         } catch (exception ex) {             ex.printstacktrace();         }         // access token refresh token         gettoken(authresult.getrefreshtoken());     }      public static void gettoken(string refreshtoken) throws ioexception {          string encoding = "utf-8";         string params = "client_id=" + clientid + "&refresh_token=" + refreshtoken                 + "&grant_type=refresh_token&resource=https%3a%2f%2fgraph.windows.net";         string path = "https://login.microsoftonline.com/" + tenantid + "/oauth2/token";         byte[] data = params.getbytes(encoding);         url url = new url(path);         httpurlconnection conn = (httpurlconnection) url.openconnection();         conn.setrequestmethod("post");         conn.setdooutput(true);         conn.setrequestproperty("content-type", "application/x-www-form-urlencoded");         conn.setrequestproperty("content-length", string.valueof(data.length));         conn.setconnecttimeout(5 * 1000);         outputstream outstream = conn.getoutputstream();         outstream.write(data);         outstream.flush();         outstream.close();         system.out.println(conn.getresponsecode());         system.out.println(conn.getresponsemessage());          bufferedreader br = null;         if (conn.getresponsecode() != 200) {             br = new bufferedreader(new inputstreamreader((conn.geterrorstream())));         } else {             br = new bufferedreader(new inputstreamreader((conn.getinputstream())));         }         system.out.println("response body : " + br.readline());     }  } 

the result printed in console below :

enter image description here

hope helps you.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -