ADALiOS AcquireToken()

时间:2016-12-31 02:35:15

标签: ios swift adal

我已经使用ADALiOS尝试了许多样本。如您所知,ADALiOS不断改变其实施方式。因为我是一个快速的初学者,所以我不知道如何从互联网工作中提取样本。

我尝试了一个版本的adal 3.0(预发行版)。 如您所见,以下代码是从下载的一个示例中复制而来的。每个样本都有一个编译器错误,例如缺少参数标签政策......'或者'无法转换ADAuthenticationResult ...'在authContext.acquireToken()或acquireTokenwithResource()方法中,无论其adal版本如何。有人可以帮帮我吗? 错误消息是关于completionBlock:....

感谢

authContext.acquireToken(withScopes: [Any](), additionalScopes: [Any](), clientId: clientId, redirectUri: redirectURL, identifier: id!, promptBehavior: prompt, extraQueryParameters: "", completionBlock: <#T##ADAuthenticationCallback!##ADAuthenticationCallback!##(ADAuthenticationResult?) -> Void#>){
        if result.status.value != AD_SUCCEEDED.value {
            // Failed, return error description
            completionHandler(false, result.error.description)
        }
        else {
            // Succeeded, return the acess token
            var token = result.accessToken
            // Initialize the dependency resolver with the logged on context.
            // The dependency resolver is passed to the Outlook library.
            self.dependencyResolver = ADALDependencyResolver(context: authContext, resourceId: self.outlookResource, clientId: self.clientId, redirectUri: self.redirectURL)
            completionHandler(true, token)
        }
    }

1 个答案:

答案 0 :(得分:1)

我刚使用adal 3.0(预发行版)。在查看示例时,看起来ADAL API已经发生了变化。我找不到acquireTokenwithResource()所以我用过:

acquireToken(withScopes:additionalScopes:clientId:redirectUri:identifier:,promptBehavior :)

我认为我遇到的最大问题是没有在这里注册原生应用:https://apps.dev.microsoft.com

在我这样做并使用apps.dev.microsoft中的clientId和redirectURI,并且还包含正确的范围之后,我能够进行身份验证并返回accessToken。

我的acquireToken看起来像这样:

 func acquireAuthToken(completion: ((AuthenticationResult) -> Void)?) {

        let identity = ADUserIdentifier(id: "Default email address", type: ADUserIdentifierType(rawValue:1))



        self.context.acquireToken(withScopes: ["User.Read"], additionalScopes: nil, clientId: AppData.sharedInstance?.clientId, redirectUri: URL(string: (AppData.sharedInstance?.redirectUriString)!), identifier: identity, promptBehavior: AD_PROMPT_AUTO) {
            (result) in

            if let handler = completion {
                if result!.status == AD_SUCCEEDED {
                    self.accessToken = result!.token

                    handler(AuthenticationResult.Success)
                }
                else {
                    handler(AuthenticationResult.Failure(result!.error))
                }
            }
        }
    }

上下文只是ADAuthenticationContext(权限:ppData.sharedInstance?.authority,.Authority,错误:&amp;错误)

HTH