1

こんにちは、私は非常に晴れた広告とOauth2を使用してGraphAPIを使用しようとしています。Azure AD - 無効な認証トークンを返すOauth2

static let tenant = "tenant.com" 
static let clientId = "22d31baa-5acf-4324-8ac1-02f0021g4f56" 
static let redirectURI = URL.init(string: "test://com.test.est") 
static let authority = "https://login.microsoftonline.com/\(tenant)/oauth2/authorize" 
static let resourceId = "https://graph.microsoft.com" 


var authContext: ADAuthenticationContext! 

func getAuth(){ 
    var error: ADAuthenticationError? = nil 
    authContext = ADAuthenticationContext(authority: Authentication.authority, error: &error) 
    authContext.acquireToken(withResource: Authentication.resourceId, clientId: Authentication.clientId, redirectUri: Authentication.redirectURI, completionBlock: {(result:ADAuthenticationResult!) in 
     if(result.accessToken == nil){ 
      //Token acquisition failed 
      print("Failed receving Token") 
     }else{ 
      //Toekn acquisition succeeded 
      let headers: HTTPHeaders = ["Authorization":"Bearer \(result.tokenCacheStoreItem.accessToken)"] 

      Alamofire.request("\(Authentication.resourceId)/me", headers: headers).responseJSON(completionHandler: { response in 
       print(response) 
      }) 

     } 
    }) 
} 

このコードは、私が手に実行され、結果:

SUCCESS: { 
error =  { 
    code = InvalidAuthenticationToken; 
    innerError =   { 
     date = "2017-05-05T22:44:39"; 
     "request-id" = "22d31baa-5acf-4324-8ac1-02f0021g4f56"; 
    }; 
    message = "CompactToken parsing failed with error code: -2147184105"; 
}; 

}

エラーメッセージがalamofire.requestの内側に印刷されて

私は現在、私のコードを持っています。 oauth2部分を消去すると、同じ結果が返されるため、私の権限が乱れているように感じます。私はoauth2をもう一度勉強しようとしますが、私のコードで何か間違いが起きているかどうかを教えてください。 ありがとうございました

+0

どのようなトークンを取得しようとしていますか? [App Only Token](https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-service-to-service)または[委任トークン]( https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-code)?私はあなたの権限があなたに認可コードを取得したいと言っているので、あなたのgetAuth()関数は認可コードを取得するプロセスを経ていないように思えます。 –

+0

ああ、申し訳ありません。関数名は無視してください。私はまだAzureが初めてで、複数の機能をテストしていました。 –

+0

私が達成したいのは、トークンを取得し、トークンを使用してGraphapiにアクセスすることです。まだ苦労しています。 –

答えて

0

最後に私はそれを管理する方法を考え出しました。

import Foundation 
import ADALiOS 
import Alamofire 


class Authentication{ 
let tenant: String 
let clientId: String 
let redirectURI: URL 
let authority: String 
let resourceId: String 

init(){ 
    tenant = "tenant" 
    clientId = "client" 
    redirectURI = URL.init(string: "uri")! 
    authority = "https://login.microsoftonline.com/\(tenant)/authorize?client_id=\(clientId)&response_type=code&redirect_uri=\(redirectURI)&response_mode=query" 
    resourceId = "https://graph.microsoft.com" 
} 

private var authContext: ADAuthenticationContext! 

private var token: String? = nil 
var response: DataResponse<Any>? = nil 


func authorize(){ 

    var error: ADAuthenticationError? = nil 
    authContext = ADAuthenticationContext(authority: authority, error: &error) 
    authContext.acquireToken(withResource: resourceId, clientId: clientId, redirectUri: redirectURI, completionBlock: {(result:ADAuthenticationResult!) in 
     if(result.accessToken == nil){ 
      //Token acquisition failed 
      print("Failed receving Authorizing Token") 
     }else{ 
      //Token acquisition succeeded 
      let headers = [ 
       "Content-Type":"application/json", 
       "Accept":"application/json, text/plain, */*", 
       "Authorization":"Bearer \(result.tokenCacheStoreItem.accessToken!)" 
      ] 
      Alamofire.request("https://graph.microsoft.com/beta/me/", headers: headers).responseJSON(completionHandler: { response in 
       self.response = response 
      }) 
     } 
    }) 
} 

基本的に、ヘッダーをいくつか追加してベータ版を使用する必要がありました。私がベータ版以外を使用すると、無効なバージョンエラーが返されます。

関連する問題