iOS Yelp OAuth 令牌检索,URLRequest 返回“client_id 或 client_secret 参数未找到
iOS Yelp OAuth Token Retrieval with URLRequest returning "client_id or client_secret parameters not found
我正在尝试从使用本机 URL 和 URL 的 iOS 客户端检索 OAuth 令牌以使用 Yelp 的 Fusion API请求 类,但它在 "tokenInfo" 变量中给我这个错误:
client_id or client_secret parameters not found. Make sure to provide
client_id and client_secret in the body with the
application/x-www-form-urlencoded content-type
这是我的代码:
func getToken(){
var yelpTokenEndpoint = "https://api.yelp.com/oauth2/token"
var tokenURL = URL(string: yelpTokenEndpoint)
let requestJSON: [String:String] = ["client_id":"Not showing actual client id", "client_secret":"Not Gonna Show My Actual Client Secret either","grant_type":"client_credentials"]
let requestData = try? JSONSerialization.data(withJSONObject: requestJSON)
print(try? JSONSerialization.jsonObject(with: requestData!, options: []))
var tokenURLRequest = URLRequest(url: tokenURL!)
tokenURLRequest.httpMethod = "POST"
tokenURLRequest.httpBody = requestData!
tokenURLRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
let tokenSession = URLSession.shared
let tokenTask = tokenSession.dataTask(with: tokenURLRequest) { (data, response, error) in
if error != nil {
print("error getting your access token")
print(error!.localizedDescription)
}
if let data = data{
do{
if let tokenInfo = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Any]{
let token: String = tokenInfo["access_token"] as! String
print(token)
}
} catch {
print("Error converting to JSON")
}
}
}
tokenTask.resume()
}
是的,我确信我输入了正确的客户端凭据。非常感谢任何帮助,谢谢!
试试这个....
let clientId = "client_id"
let clientSecret = "client_secret"
let tokenURL = "https://api.yelp.com/oauth2/token"
let grantType = "client_credentials"
let url = NSURL(string: tokenURL as String );
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let request = NSMutableURLRequest(URL: NSURL(string: tokenURL)!)
request.HTTPMethod = "POST";
request.HTTPShouldHandleCookies = true
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let postString = "client_id=" + clientId + "&client_secret=" + clientSecret + "&grant_type=" + grantType
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task: NSURLSessionDataTask = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
if let data = data {
let response = NSString(data: data, encoding: NSUTF8StringEncoding)
print(response)
}
}
task.resume()
我正在尝试从使用本机 URL 和 URL 的 iOS 客户端检索 OAuth 令牌以使用 Yelp 的 Fusion API请求 类,但它在 "tokenInfo" 变量中给我这个错误:
client_id or client_secret parameters not found. Make sure to provide
client_id and client_secret in the body with the
application/x-www-form-urlencoded content-type
这是我的代码:
func getToken(){
var yelpTokenEndpoint = "https://api.yelp.com/oauth2/token"
var tokenURL = URL(string: yelpTokenEndpoint)
let requestJSON: [String:String] = ["client_id":"Not showing actual client id", "client_secret":"Not Gonna Show My Actual Client Secret either","grant_type":"client_credentials"]
let requestData = try? JSONSerialization.data(withJSONObject: requestJSON)
print(try? JSONSerialization.jsonObject(with: requestData!, options: []))
var tokenURLRequest = URLRequest(url: tokenURL!)
tokenURLRequest.httpMethod = "POST"
tokenURLRequest.httpBody = requestData!
tokenURLRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
let tokenSession = URLSession.shared
let tokenTask = tokenSession.dataTask(with: tokenURLRequest) { (data, response, error) in
if error != nil {
print("error getting your access token")
print(error!.localizedDescription)
}
if let data = data{
do{
if let tokenInfo = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Any]{
let token: String = tokenInfo["access_token"] as! String
print(token)
}
} catch {
print("Error converting to JSON")
}
}
}
tokenTask.resume()
}
是的,我确信我输入了正确的客户端凭据。非常感谢任何帮助,谢谢!
试试这个....
let clientId = "client_id"
let clientSecret = "client_secret"
let tokenURL = "https://api.yelp.com/oauth2/token"
let grantType = "client_credentials"
let url = NSURL(string: tokenURL as String );
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let request = NSMutableURLRequest(URL: NSURL(string: tokenURL)!)
request.HTTPMethod = "POST";
request.HTTPShouldHandleCookies = true
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let postString = "client_id=" + clientId + "&client_secret=" + clientSecret + "&grant_type=" + grantType
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task: NSURLSessionDataTask = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
if let data = data {
let response = NSString(data: data, encoding: NSUTF8StringEncoding)
print(response)
}
}
task.resume()