简体   繁体   English

Azure AD - Oauth2返回无效的身份验证令牌

[英]Azure AD - Oauth2 returning Invalid Authentication Token

Hi I am very new to azure ad and trying to use GraphAPI using Oauth2. 嗨,我对azure广告很新,并试图使用Oauth2使用GraphAPI。

I currently have my code: 我目前有我的代码:

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)
            })

        }
    })
}

When this code is executed i get, result: 当我执行此代码时,结果:

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

} }

The error message is printed inside alamofire.request. 错误消息打印在alamofire.request中。 I feel like my authority is messed up because when I erase oauth2 portion, it still returns the same result. 我觉得我的权威搞砸了,因为当我删除oauth2部分时,它仍然会返回相同的结果。 I try studying oauth2 again but let me know if any mistake going on in my code. 我再次尝试学习oauth2,但如果我的代码中出现任何错误,请告诉我。 Thanks a lot 非常感谢

Finally I figured out how to manage it. 最后我想出了如何管理它。

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
            })
        }
    })
}

Basically, I had to add some headers and use beta. 基本上,我必须添加一些标题并使用beta。 If i use other than beta, it returns invalid version error. 如果我使用的不是beta,则会返回无效的版本错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM