繁体   English   中英

http标头中的Yelp身份验证

[英]Yelp authentication in http headers

我只是尝试使用yelp api( https://www.yelp.com/developers/documentation/v2/authentication )搜索业务位置。 但是,尽管向标头添加了必填字段,但我无法对其服务进行身份验证。 这是代码:

    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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM