Forum Discussion
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
- robert_karenNew Member
to be clear the authorization token I am trying to get is for Azure AD. Thanks.
- Greg_DecklerCommunity Champion
http://www.cloudidentity.com/blog/2013/07/23/securing-a-web-api-with-windows-azure-ad-and-katana/
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>- robert_karenNew Member
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.