【发布时间】:2017-07-21 22:50:18
【问题描述】:
您好,我对 azure ad 非常陌生,并尝试使用 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,但如果我的代码中出现任何错误,请告诉我。 非常感谢
【问题讨论】:
-
你想得到什么样的令牌? App Only Token 还是 Delegated Token?我问是因为您的授权说您想要获得授权码,但是您的 getAuth() 函数似乎没有经过获得授权码的过程。
-
对不起。请忽略函数名。我还是 Azure 的新手,并且正在测试该函数中的多项内容。
-
我想要实现的是获取令牌并使用令牌并访问 Graphapi.... 仍在苦苦挣扎。
-
你看过我们的End to End samples吗?您是否尝试过运行graph tokens with PowerShell 的简单“hello world”?
标签: ios swift azure azure-mobile-services azure-active-directory