(中国)
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", "[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.サービスプリンシパル認証
サービスプリンシパル認証プロセスリファレンス:
https:
//blog.csdn.net/leinminna/article/details/109101761 powerbiアプリ権限設定リファレンス: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. OAuth2.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認証コードを介してトークンを取得する
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;
}
}