powerbi 获取access token

本文详细介绍了在中国区使用PowerBI的三种认证方式:密码凭证、服务主体凭证和OAuth2.0授权码。包括getPasswordToken()、getEmailToken()和getXxxCode()方法,以及POST请求和参数配置。
摘要由CSDN通过智能技术生成

(中国区)

1.用户名密码认证

	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", "xxx@lenovocloudbroker.partner.onmschina.cn");
		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.服务主体认证

服务主体认证流程参考:
https://blog.csdn.net/leinminna/article/details/109101761
powerbi app 权限设置参考:
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授权码认证

注意:首次登录也需要输入用户名和密码;
参考:
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请求
在这里插入图片描述
解析重定向地址中的授权码(code=xxx),暂无解析代码
在这里插入图片描述

3.2 通过授权码获取token
    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. 方法

前面省略的方法

	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;
	}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
本地服务获取access token的配置需要进行以下几个步骤: 1. 注册开发者账号并创建应用:首先,在CSDN开发者平台注册一个开发者账号,并创建一个应用。创建应用时,你会获得一对Client ID和Client Secret,这是用来获取access token的凭证。 2. 配置回调地址:在应用创建完成后,你需要配置一个回调地址。回调地址是在用户授权后重定向的URL,用于接收授权码。 3. 配置本地服务器:在本地服务器上,你需要创建一个接收回调请求的API端点。可以使用任何后端框架或库来创建一个HTTP服务,监听指定的端口,并处理来自CSDN的授权回调请求。 4. 接收授权码:当用户授权完成后,CSDN将会将授权码作为参数附加到回调URL中,然后重定向到你之前设置的回调地址。在你的本地服务器的API端点中,解析URL中的授权码。 5. 通过授权码获取access token:使用之前获取的授权码,向CSDN的Access Token URL发送一个POST请求,以获取access token。请求中需要包含Client ID、Client Secret、授权码以及其他必要的参数。 6. 存储和使用access token:一旦成功获取access token,将其存储在本地安全的位置。在每次使用CSDN API时,将access token作为Authorization头部的Bearer Token传递给API服务器。 请注意,保护好你的Client Secret和access token是非常重要的,不要将其泄露给其他人。另外,确保你的本地服务器是安全的,以防止未经授权的访问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值