(中国区)
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;
}
}