1

嗨我对Azure广告很陌生,尝试使用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) 
      }) 

     } 
    }) 
} 

当执行该代码我得到的,结果是:打印内部alamofire.request

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"; 
}; 

}

错误消息。 我觉得我的权威被搞砸了,因为当我删除oauth2部分时,它仍然返回相同的结果。我试着再次学习oauth2,但是让我知道在我的代码中是否有任何错误。 非常感谢

+0

你试图得到什么样的标记? [App Only Token](https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-service-to-service)或[Delegated Token]( 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 
      }) 
     } 
    }) 
} 

基本上,我不得不添加一些标题和使用测试版。如果我使用beta以外的版本,则会返回无效的版本错误。