ios - Azure AD - Oauth2 返回无效的身份验证 token

标签 ios swift azure azure-mobile-services azure-active-directory

您好,我对 Azure 广告非常陌生,并尝试使用 Oauth2 来使用 GraphAPI。

我目前有我的代码:

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,但如果我的代码中出现任何错误,请告诉我。 非常感谢

最佳答案

终于我弄清楚了如何管理它。

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

基本上,我必须添加一些 header 并使用测试版。如果我使用 beta 以外的版本,它会返回无效版本错误。

关于ios - Azure AD - Oauth2 返回无效的身份验证 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43850539/

相关文章:

ios - NSLog 输出消息在控制台中重复两次(重复)

ios - Restkit-如何在表格 View 中显示 json 数据

swift - 这个快速声明在做什么?

ios - 同时更改 UILabel 文本和位置不起作用

azure - 使用 azure 构建管道、自托管代理和 docker-desktop 创建 docker 镜像

ios - 快速检查未初始化的数组

ios - 在登录成功时执行 segue

swift - "Constrain to margins"在 Xcode 6.0.1 的 Interface Builder 中意味着什么

azure - 使用 Azure Front Door 提供 SPA 服务

windows - 命令提示符 ssms.exe 使用 Windows 身份验证登录