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,但是讓我知道在我的代碼中是否有任何錯誤。 非常感謝
你試圖得到什麼樣的標記? [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()函數似乎沒有通過獲得授權碼的過程。 –
對不起。請忽略功能名稱。我對Azure仍然很陌生,並且正在測試函數中的多個事物。 –
我想要實現的是獲得令牌並使用令牌並訪問Graphapi ....仍在掙扎。 –