Azure访问令牌无法正常工作

时间:2017-07-19 06:33:56

标签: .net node.js azure azure-web-sites azure-active-directory

下面的代码完全正常工作,并从azure返回所需的访问令牌,但如果我尝试从节点js或postman执行相同的功能,则会提示错误:

  

{“error”:“invalid_client”,“error_description”:“AADSTS70002:错误   验证凭据。 AADSTS50012:无效的客户端密钥是   提供。\ r \ nTrace 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], ”时间戳“:” 2017年7月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;

1 个答案:

答案 0 :(得分:0)

对我而言,{ if (isset($_SERVER['HTTP_CLIENT_IP'])) $ipaddress = $_SERVER['HTTP_CLIENT_IP']; else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; else if(isset($_SERVER['HTTP_X_FORWARDED'])) $ipaddress = $_SERVER['HTTP_X_FORWARDED']; else if(isset($_SERVER['HTTP_FORWARDED_FOR'])) $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; else if(isset($_SERVER['HTTP_FORWARDED'])) $ipaddress = $_SERVER['HTTP_FORWARDED']; else if(isset($_SERVER['REMOTE_ADDR'])) $ipaddress = $_SERVER['REMOTE_ADDR']; else $ipaddress = 'UNKNOWN'; $macCommandString = "arp " . $ipaddress . " | awk 'BEGIN{ i=1; } { i++; if(i==3) print $3 }'"; $mac = exec($macCommandString); return ['ip' => $ipaddress, 'mac' => $mac]; } API始终运作良好。我认为你正在使用的类没有正确编码值。

HttpClient