2017-07-18 41 views
2

我正在嘗試使用AZURE AD進行身份驗證我從git [https://github.com/Azure-Samples/active-directory-java-webapp-openidconnect][1]AZURE AD ADAL「error」:「invalid_grant」,「error_description」:「AADSTS70000:傳輸數據解析器失敗:授權碼格式不正確或無效

我能夠讓授權的呼叫並獲得授權碼。通過傳遞授權碼使用來自的oauth2罐子acquireTokenByAuthorizationCode方法來獲得訪問令牌。這裏我提示以下錯誤。

「error」:「invalid_grant」,「error_description」:「AADSTS70000:傳輸數據解析器失敗:授權碼爲m變形或無效。

如何將grant_type = authorization_code傳遞給acquireTokenByAuthorizationCode方法?

如何檢查發佈的帖子請求?我無法在Chrome的網絡部分看到它?

下面是代碼:

 String authCode = authorizationCode.getValue(); 
     ClientCredential credential = new ClientCredential(clientId, 
       clientSecret); 
     AuthenticationContext context; 
     AuthenticationResult result = null; 
     ExecutorService service = null; 
     try { 

      ThreadFactory factory = ThreadManager.currentRequestThreadFactory(); 
      service = Executors.newCachedThreadPool(factory); 

      context = new AuthenticationContext(authority + tenant + "/", true, 
        service); 

      Future<AuthenticationResult> future = context 
        .acquireTokenByAuthorizationCode(authCode, new URI(
          currentUri), credential, null); 

POST請求應該是: 字符串的redirectUrl =權威 + this.tenant +「/oauth2/v2.0/token P = b2c_1_abcd &? grant_type = authorization_code &資源= HTTPS%3A%2F%2fgraph.windows.net & REDIRECT_URI =」 + URLEncoder.encode(REDIRECT_URL,「UTF-8」);

不確定如何提供粗體信息。

+0

通過在請求授權碼時通過scope = offline,我可以獲得刷新令牌。我是否需要進行任何此類更改以獲取訪問令牌? –

回答

1

後編輯的詳細信息

如果您正在使用V2終點,你不能使用ADAL。

如果您在v2開發者門戶中註冊了您的應用程序,則需要通過Azure門戶的Azure AD接口註冊您的應用程序。然後確保您的Azure AD網址不包含v2.0。


要取得授權碼一個道理,你用acquireTokenByAuthorizationCodehttps://github.com/Azure-Samples/active-directory-java-webapp-openidconnect/blob/master/src/main/java/com/microsoft/aad/adal4jsample/BasicFilter.java#L268-L270):

Future<AuthenticationResult> future = context 
       .acquireTokenByAuthorizationCode(authCode, new URI(
         currentUri), credential, null); 

你不會在Chrome中看到的請求,因爲它從你的Web服務器Azure的AD去。瀏覽器不是鏈中的一部分。 這是一件好事,因爲我們將客戶端密鑰傳遞給Azure AD。您可以使用Fiddler等工具來監控流量。

+0

您可以添加您用來接收授權碼的問題嗎? – juunas

+0

添加了代碼@juunas –

+0

@krishnapriyanukala查看我的編輯。如果您嘗試在ADAL中使用v2端點,則無法工作。 – juunas

相關問題