如何将此 oAuth curl 请求转换为 swift 语法。
curl -X POST -vu clientapp:123456 http://localhost:8080/oauth/token -H "Accept: application/json" -d "password=spring&username=roy&grant_type=password&scope=read%20write&client_secret=123456&client_id=clientapp"
最佳答案
我能够通过在 base 64 字符串中编码用户凭证来实现这一点。
let parameters = ["password" : "spring", "username" : "roy", "grant_type" : "password",
"scope" : "read write", "client_secret" : "123456", "client_id" : "clientapp"]
let str = "clientapp:123456"
let utf8str = str.dataUsingEncoding(NSUTF8StringEncoding)
let basic_auth_token = utf8str?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
let headers = ["Accept" : "application/json", "Authorization" :"Basic "+basic_auth_token!]
Alamofire.request(.POST, "http://localhost:8080/oauth/token", parameters: parameters, encoding:.URL, headers: headers)
.responseJSON { response in
print("Response String: \(response.result.value)")
}
关于swift - 从 Swift iOS 发出 oAuth 身份验证请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35795753/