【问题标题】:Azure access token not workingAzure 访问令牌不起作用
【发布时间】: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


【解决方案1】:

对我来说,HttpClient API 总是很好用。我认为您使用的类没有正确编码值。

// Static field within class to share the same client instance
private static HttpClient Client = new HttpClient();

public async Task<string> GetAccessTokenAsync()
{
    //Get the environment variable from somewhere

    var request = new HttpRequestMessage(HttpMethod.Post, "https://login.microsoftonline.com/" + environment.getTenantId() + "/oauth2/token");

    var keyValues = new List<KeyValuePair<string, string>>();
    keyValues.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
    keyValues.Add(new KeyValuePair<string, string>("client_id", environment.getClientId()));
    keyValues.Add(new KeyValuePair<string, string>("client_secret", environment.getClientSecret()));
    keyValues.Add(new KeyValuePair<string, string>("resource", "https://graph.windows.net"));

    request.Content = new FormUrlEncodedContent(keyValues);

    HttpResponseMessage response = await Client.SendAsync(request);

    string json = await response.Content.ReadAsStringAsync();

    JObject tokenResponse = JObject.Parse(json);

    string accessToken = tokenResponse["access_token"];
    return accessToken;
}

【讨论】:

  • 感谢回复
猜你喜欢
  • 2017-03-25
  • 2019-09-26
  • 2013-05-28
  • 2015-10-19
  • 2020-03-09
  • 2016-05-23
  • 1970-01-01
  • 1970-01-01
  • 2023-02-02
相关资源
最近更新 更多