gpt4 book ai didi

swift - http header 中的 Yelp 身份验证

转载 作者:行者123 更新时间:2023-11-28 08:18:54 27 4
gpt4 key购买 nike

我只是尝试使用 yelp api ( https://www.yelp.com/developers/documentation/v2/authentication ) 搜索营业地点。但是,尽管在 header 中添加了必填字段,但我无法通过他们的服务进行身份验证。这是代码:

    private func generateAuthorizationHeaders() -> String {
let consumer = "oauth_consumer_key=\"\(consumerKey!)\""
let accessToken = "oauth_token=\"\(token!)\""
let signatureMethod = "oauth_signature_method=\"HMAC-SHA1\""
let signature = "oauth_signature=\"\(token!.hmacsha1(key: tokenSecret!))\"" //More than likely wrong
let timestamp = "oauth_timestamp=\"\(Int(Date.timeIntervalBetween1970AndReferenceDate))\""
let uniqueString = "oauth_nonce=\"nvgnhvfvfvffvvf\""
return "\(consumer),\(accessToken),\(signatureMethod),\(signature),\(timestamp),\(uniqueString)"
}

func search(for place: String, at location: CLLocationCoordinate2D) {
let authorization = generateAuthorizationHeaders()
let string = "https://api.yelp.com/v2/search?term=\(place)&ll=\(location.latitude),\(location.longitude)"
let request = NSMutableURLRequest(url: NSURL(string: string) as! URL)
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("\(authorization)", forHTTPHeaderField: "Authorization")

URLSession.shared.dataTask(with: request as URLRequest, completionHandler: {data, response, error in
... JSON stuff
}).resume()

我收到的错误信息是:

{
error = {
field = "oauth_consumer_key";
id = "MISSING_PARAMETER";
text = "One or more parameters are missing in request";
};

有人能发现问题吗?谢谢。

最佳答案

*将近两年后*

看起来您缺少 OAuth

试试这个:

return "OAuth \(consumer),\(accessToken),\(signatureMethod),\(signature),\(timestamp),\(uniqueString)

关于swift - http header 中的 Yelp 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41795074/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com