Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
robert_karen
New Member

having trouble getting oauth2 token from azure w/ java

Hi,

 

I am new to developing BI apps and am stuck trying to get authentication for a web server app via azure.

I am getting an authentication code, but when I try to request an authentication token I only get http response

code 400. I am using the java code below. can someone tell me what I'm doing wrong? When I send it to my own

server and read the values it looks ok, but azure sends me back error 400. Thanks for any ideas!

 

	private void doRequestToken() {
		showMsg("begin");
		String u = "https://login.microsoftonline.com/common/oauth2/token";
		//String u = "http://load.crosstab.com:8080/cgi-bin/test-cgi.tcl";
		try {
			//System.out.println("beginning RequestToken...");
			String code = Executions.getCurrent().getParameter("code");
			showMsg("code = " + code);
			String access_token;
			if (code != null && !code.isEmpty()) {
				String rawData = "code=" + code;
				rawData += "&grant_type=authorization_code";
				rawData += "&client_id=b10be962-4d37-4017-abbb-afee851e5c61";
				rawData += "&redirect_uri=http%3A%2F%2Fload.crosstab.com%3A9090%2FezTAB2BIGetToken%2F";
				//rawData += "&redirect_uri=http://load.crosstab.com:9090/ezTAB2BIGetToken/";
				//String type = "application/x-www-form-urlencoded";
				//String encodedData = URLEncoder.encode( rawData, "UTF-8" );
				URL url = new URL(u);
				showMsg("opening connection to " + u);
				HttpURLConnection conn = (HttpURLConnection) url.openConnection();
				conn.setDoOutput(true);
				conn.setRequestMethod("POST");
				conn.setRequestProperty("User-Agent", USER_AGENT);
				conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
				//conn.setRequestProperty("Content-type", type);
				//conn.setRequestProperty("Content-length", String.valueOf(rawData.length()));
				DataOutputStream os = new DataOutputStream(conn.getOutputStream());
				os.writeBytes(rawData);
				os.flush();
				os.close();
				BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
				String line;
				while ((line = in.readLine()) != null) {
					showMsg("received: " + line);
				}
7 REPLIES 7
robert_karen
New Member

to be clear the authorization token I am trying to get is for Azure AD. Thanks.

http://www.cloudidentity.com/blog/2013/07/23/securing-a-web-api-with-windows-azure-ad-and-katana/

 

 

http://stackoverflow.com/questions/18437173/how-to-get-access-token-after-windows-azure-active-direc...

 

ClaimsPrincipal cp = ClaimsPrincipal.Current;
ClaimsIdentity ci = cp.Identity as ClaimsIdentity;
BootstrapContext bc = ci.BootstrapContext as BootstrapContext;
SecurityToken securityToken = bc.SecurityToken;

You also need to set the saveBootstrapContext attribute in your config file:

<system.identityModel>
    <identityConfiguration saveBootstrapContext="true">
    ...
</system.identityModel>

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thanks, but these don't address my question. I am trying to use a simple Java program to get authorization

token from Azure AD in order to use Power BI RESt api. I am having trouble  as I described above. Thanks again.

Have you looked at the body of the 400 response to see if there's any additional information?

Thanks for responding.

Somehow the http 400 throws a java IOException. In the exception code I tried reading getErrorStream() from the

HttpURLConnection ('conn' in my code above) but nothing meaningful came down. I know there is supposed to be a json

returned w/ the 400. getInputStream() didn't return anything either. Do you know how to get to it in java?

 

.

I'm sorry, I'm not familiar with the Java HTTP libraries.

Maybe you can clarify that I am using correct resource_uri. The one I have registerred in azure is the one included in the request for the auth. code, and it is where the code

response is sent. then that same web app which received the code is making the request for the token, which contains the same resource_uri  parameter as in the request-

 

so the web app requesting the token includes a resource_uri which is 'itself'. is that correct? it doesn't sound right. 

 

thanks for any help?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors