【发布时间】:2018-01-05 00:19:43
【问题描述】:
以下代码运行良好,并从 azure 返回所需的访问令牌,但如果我尝试从节点 js 或邮递员执行相同的功能,则会提示错误:
{"error":"invalid_client","error_description":"AADSTS70002: 错误 验证凭据。 AADSTS50012:无效的客户端密码是 已提供。\r\n跟踪 ID: 922f61ca-0349-47fc-8c60-326cb29b2000\r\n相关 ID: 3d39e54d-deb2-49de-84c0-9705e2977c2e\r\n时间戳:2017-07-18 14:29:14Z","error_codes":[70002,50012],"timestamp":"2017-07-18 14:29:14Z","trace_id":"922f61ca-0349-47fc-8c60-326cb29b2000","correlation_id":"3d39e54d-deb2-49de-84c0-9705e2977c2e"}
但在 java 环境中可以多次使用同样的效果
HttpPost httpPost = new HttpPost("https://login.microsoftonline.com/" + environment.getTenantId() + "/oauth2/token");
List<NameValuePair> nameValuePairs = new ArrayList(3);
nameValuePairs.add(new BasicNameValuePair("grant_type", "client_credentials"));
nameValuePairs.add(new BasicNameValuePair("client_id", environment.getClientId()));
nameValuePairs.add(new BasicNameValuePair("client_secret", environment.getClientSecret()));
nameValuePairs.add(new BasicNameValuePair("resource", "https://graph.windows.net"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
HttpResponse response = httpClient.execute(httpPost);
String postResponse = EntityUtils.toString(response.getEntity());
String startPoint = "\"access_token\":\"";
int startIndex = postResponse.indexOf(startPoint);
int adjustPoint = startIndex + startPoint.length();
String objectId = postResponse.substring(adjustPoint);
int tokenLength = objectId.length();
String accessToken = objectId.substring(0, tokenLength - 2);
return accessToken;
【问题讨论】:
-
找到解决方案,我是从正文发送选项,但它应该来自 FormData。
标签: .net node.js azure azure-web-app-service azure-active-directory