Forum Discussion

robert_karen's avatar
robert_karen
New Member
11 years ago

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