2017-10-27 16 views
0

Javaで単純なアンドロイドアプリを作成しています。最近、Microsoft Dynamics CRMからユーザーのトークンを取得する実装を行いました(私はAzureで接続済みのアプリケーションを作成しました。 、秘密など)。Dynamics CRMトークン(401 Unauthorized)を使用できません。

私は、このアプリケーションの他のユーザーがCRMや組織に接続できるようにしたいと考えています。

今、REST APIでトークンを使用して401エラーを取得しようとしています。 ここに関連するすべての回答を読んでください。何も助けられませんでした。私が使用しているコード:

//retrieved the authorization code by this url: 
mAuthorizationUrl = Configuration.AUTHORIZE_ENDPOINT + "?response_type=code&client_id=" 
+ Configuration.CLIENT_ID + "&redirect_uri=" + Configuration.REDIRECT_URI; 

... 

//Retrieving access_token: 
String body_content = "grant_type=authorization_code&client_id=" + 
Configuration.CLIENT_ID + "&redirect_uri=" + Configuration.REDIRECT_URI 
+ "&code=" + code + "&resource=" + Configuration.CLIENT_ID; 
//I don't have app URI (resource) in Azure, so I used app id (client id). 
//This worked (see above). 

RequestBody body = RequestBody.create(
        MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"), 
        body_content); 
Request request = new Request.Builder() 
.url(Configuration.TOKEN_RETRIEVAL_ENDPOINT) 
.post(body) 
.build(); 

Response response = new OkHttpClient().newCall(request).execute(); 

String responseString = response.body().string(); 
JSONObject json = new JSONObject(responseString); 
String token = json.getString("access_token"); 

//NOT WORKING CODE: 
OkHttpClient okHttpClient = new OkHttpClient().newBuilder() 
.protocols(Collections.singletonList(Protocol.HTTP_1_1)) 
.build(); 

Map<String, String> headers = new ArrayMap<>(); 
headers.put("Authorization", "Bearer " + token)); 
headers.put("Accept", "application/json"); 

request = new Request.Builder() 
.url(Configuration.REST_ENDPOINT) 
.headers(Headers.of(headers)) 
.build(); 
try { 
    response = okHttpClient 
    .newCall(request) 
    .execute(); 
    statusCode = response.code(); 
} 
... 

//401 UNAUTHORIZED 

エンドポイントは、私が使用:

AUTHORIZE_ENDPOINT = https://login.microsoftonline.com/common/oauth2/authorize

TOKEN_RETRIEVAL_ENDPOINT = https://login.microsoftonline.com/common/oauth2/token

REST_ENDPOINT = url_to_crm /api/data/v9.0/

+0

このリクエストは、RESTクライアント(Advanced RESTクライアントなど)を使用して実行しようとしましたか?このようなクライアントは、REST APIを調査してリクエストを確認するのに適しています。 – Boris

答えて

1

ここには、接続して認証する2つのサンプルJavaプロジェクトがありますアズール経由ダイナミクスのWeb APIとケイト:

Link 1

Link 2

は、この情報がお役に立てば幸いです。

関連する問題