【发布时间】:2019-07-30 10:25:48
【问题描述】:
在没有 microsoft azure 用户的情况下使用 swift 获取访问令牌时遇到问题。 我的函数基于https://docs.microsoft.com/en-us/graph/auth-v2-service#4-get-an-access-token,如下所示:
let json: [String: Any] =
[
"grant_type": "client_credentials",
"client_id": myAppClientID,
"resource": "https://graph.microsoft.com",
"client_secret": myClientSecret
]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
let url = URL(string: "https://login.microsoftonline.com/" + myDirectoryID + "/oauth2/v2.0/token")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("Host", forHTTPHeaderField: "login.microsoftonline.com")
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseJSON = responseJSON as? [String: Any] {
print(responseJSON)
}
}
task.resume()
但我得到错误:[“error”:invalid_request,“error_description”:AADSTS900144:请求正文必须包含以下参数:'grant_type'。
【问题讨论】:
-
看一看。希望这能解决您的问题
标签: swift azure microsoft-graph-api