powerbi get access token

(China)

1. User name and password authentication

	public String getPasswordToken() {
    
    

		String token = "";
		String url = "https://login.partner.microsoftonline.cn/common/oauth2/token";

		Map<String, String> header = new HashMap<>(16);
		header.put("Content-Type", "application/x-www-form-urlencoded");
		header.put("Accept", "*/*");

		Map<String, String> body = new HashMap<>(16);
		body.put("resource", "https://analysis.chinacloudapi.cn/powerbi/api");
		body.put("client_id", "xxx");
		body.put("client_secret", "xxx");
		body.put("grant_type", "password");
		body.put("username", "[email protected]");
		body.put("password", "xxx");

		StringBuilder bodyStr = new StringBuilder();
		int i = 0;
		for (Map.Entry<String, String> entry : body.entrySet()) {
    
    
			i++;
			bodyStr.append(entry.getKey()).append("=").append(entry.getValue());
			if (i < body.size()) {
    
    
				bodyStr.append("&");
			}
		}

		String baseStr = doPost(url, header, bodyStr.toString());

		JSONObject jsonObject = JSONObject.parseObject(baseStr);
		token = jsonObject.getString("access_token");

		return token;
	}

2. Service principal authentication

Service principal authentication process reference:
https://blog.csdn.net/leinminna/article/details/109101761
powerbi app permission setting reference:
https://docs.azure.cn/zh-cn/articles/azure-operations-guide /power-bi-embedded/aog-power-bi-embedded-qa-creation-issue

	private String getEmailToken(){
    
    
		
		String token = "";
		String url = "https://login.chinacloudapi.cn/{tenantId}/oauth2/token";

		Map<String, String> header = new HashMap<>(16);
		header.put("Content-Type", "application/x-www-form-urlencoded");
		header.put("Accept", "*/*");

		Map<String, String> body = new HashMap<>(16);
		body.put("grant_type", "client_credentials");
		body.put("resource", "https://analysis.chinacloudapi.cn/powerbi/api");
		body.put("client_id", "xxx");
		body.put("client_secret", "xxx");

		StringBuilder bodyStr = new StringBuilder();
		int i = 0;
		for (Map.Entry<String, String> entry : body.entrySet()) {
    
    
			i++;
			bodyStr.append(entry.getKey()).append("=").append(entry.getValue());
			if (i < body.size()) {
    
    
				bodyStr.append("&");
			}
		}

		String baseStr = doPost(url, header, bodyStr.toString());

		JSONObject jsonObject = JSONObject.parseObject(baseStr);
		token = jsonObject.getString("access_token");

		return token;
	}

3. OAuth 2.0 authorization code authentication

Note: You also need to enter the user name and password for the first login;
reference:
https://blog.csdn.net/qq_16313575/article/details/88989160?utm_medium=distribute.pc_relevant.none-task-blog-title-1&spm=1001.2101.3001.4242

3.1 Get authorization code

Initiate a get request to
Insert picture description here
parse the authorization code (code=xxx) in the redirect address, there is no parsing code yet
Insert picture description here

3.2 Obtain token through authorization code

    public String getXxxCode() {
    
    

        String token = "";
        String url = "https://login.chinacloudapi.cn/common/oauth2/token";

        Map<String, String> header = new HashMap<>(4);
        header.put("Content-Type", "application/x-www-form-urlencoded");
        header.put("Accept", "*/*");

        Map<String, String> body = new HashMap<>(8);
        body.put("resource", "https://analysis.chinacloudapi.cn/powerbi/api");
        body.put("client_id", "xxx");
        body.put("client_secret", "xxx");
        body.put("redirect_uri", "https://xxx.xxx.com");

        body.put("grant_type", "authorization_code");
        // 获取授权码方法,思路:重定向到指定接口,保存redis,再从redis中获取
        String code = getAuthorizationCode();
        body.put("code", code);

        StringBuilder bodyStr = new StringBuilder();
        int i = 0;
        for (Map.Entry<String, String> entry : body.entrySet()) {
    
    
            i++;
            bodyStr.append(entry.getKey()).append("=").append(entry.getValue());
            if (i < body.size()) {
    
    
                bodyStr.append("&");
            }
        }

        String baseStr = doPost(url, header, bodyStr.toString());

        JSONObject jsonObject = JSONObject.parseObject(baseStr);
        token = jsonObject.getString("access_token");

        return token;

    }

3. Method

The previously omitted method

	public String doPost(String url, Map<String, String> header, String body) {
    
    

		String result = "";
		BufferedReader in = null;
		PrintWriter out = null;
		try {
    
    
			// 设置 url
			URL realUrl = new URL(url);
			HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
			connection.setRequestMethod("POST");
			// 设置 header
			for (Map.Entry<String, String> entry : header.entrySet()) {
    
    
				connection.setRequestProperty(entry.getKey(), entry.getValue());
			}
			// 设置请求 body
			connection.setDoOutput(true);
			connection.setDoInput(true);

			//设置连接超时和读取超时时间
			connection.setConnectTimeout(20000);
			connection.setReadTimeout(20000);
			try {
    
    
				out = new PrintWriter(connection.getOutputStream());
				// 保存body
				out.print(body);
				// 发送body
				out.flush();
			} catch (Exception e) {
    
    
				e.printStackTrace();
			}
			try {
    
    
				// 获取响应body
				in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
				String line;
				while ((line = in.readLine()) != null) {
    
    
					result += line;
				}
			} catch (Exception e) {
    
    
				e.printStackTrace();
			}
		} catch (Exception e) {
    
    
			e.printStackTrace();
		}
		return result;
	}
}

Guess you like

Origin blog.csdn.net/leinminna/article/details/109100731