我想使用ResteasyClient访问电源Bi中的组列表,身份验证的形式我想使用服务主体(仅应用程序令牌)。我有应用程序ID(客户端),应用程序秘密(密钥)范围= Group.Read.All,访问令牌URL https://login.microsoftonline.com/common/oauth2/token和授予类型=客户端凭据。
public static String getAccessToken(OAuth2Details oauthDetails) {
HttpPost post = new HttpPost(oauthDetails.getAuthenticationServerUrl());
String clientId = oauthDetails.getClientId();
String clientSecret = oauthDetails.getClientSecret();
String scope = oauthDetails.getScope();
List<BasicNameValuePair> parametersBody = new ArrayList<BasicNameValuePair>();
parametersBody.add(new BasicNameValuePair(OAuthConstants.GRANT_TYPE,
oauthDetails.getGrantType()));
parametersBody.add(new BasicNameValuePair(OAuthConstants.CLIENT_ID,
clientId));
parametersBody.add(new BasicNameValuePair(
OAuthConstants.CLIENT_SECRET, clientSecret));
if (isValid(scope)) {
parametersBody.add(new BasicNameValuePair(OAuthConstants.SCOPE,
scope));
}
HttpClient client = new DefaultHttpClient();
HttpResponse response = null;
String accessToken = null;
try {
post.setEntity(new UrlEncodedFormEntity(parametersBody, HTTP.UTF_8));
response = client.execute(post);
int code = response.getStatusLine().getStatusCode();
if (code == OAuthConstants.HTTP_UNAUTHORIZED) {
if …
Run Code Online (Sandbox Code Playgroud)