如何在OAuthSwift中使用YELP API?

时间:2016-02-15 06:35:55

标签: swift api oauth-2.0 token yelp

我正在尝试将OAuthSwift与Yelp API一起使用。其中一个必需参数是requestTokenURL,authorizeURL,accessTokenURL;但是,我已经从Yelp获得了令牌,并且没有Yelp的授权URL。不包括这些参数给我一个错误。将OAuthSwift与Yelp API一起使用的正确方法是什么?谢谢。

 func OAuthVerify(){
    let oauthswift = OAuth1Swift(
        consumerKey: "#",
        consumerSecret: "#",
        requestTokenUrl: "???",
        authorizeUrl:    "???",
        accessTokenUrl:  "???"
    )
    oauthswift.authorizeWithCallbackURL(NSURL(string: "oauth-swift://oauth-callback/")!, success: { (credential, response, parameters) -> Void in
        self.grabYelpData(oauthswift,consumerKey: "RZKQlWV3nqdB-74fZZRQeg")
        }) { (error) -> Void in
            print("error")
    }
}

func grabYelpData(oauthswift: OAuth1Swift, consumerKey: String){
    let url :String = "https://api.yelp.com/v2/search?"
    let parameters :Dictionary = [
        "term"           : "food",
        //"api_key"        : consumerKey,
        "location"       : "San Francisco",
    ]
    oauthswift.client.get(url, parameters: parameters,
        success: {
            data, response in
            let jsonDict: AnyObject! = try? NSJSONSerialization.JSONObjectWithData(data, options: [])
            print(jsonDict)
        }, failure: { error in
            print(error)
    })
}

1 个答案:

答案 0 :(得分:0)

我一直在寻找相同的东西,这是我迄今为止发现的唯一的东西,似乎可以工作并从Yelp返回正确的数据。

let oauthswift  = OAuth1Swift(
        consumerKey: "*********",
        consumerSecret: "*************",
        requestTokenUrl: "https://www.flickr.com/services/oauth/request_token",
        authorizeUrl:    "https://www.flickr.com/services/oauth/authorize",
        accessTokenUrl:  "https://www.flickr.com/services/oauth/access_token"
    )

    oauthswift.client.credential.oauth_token =  "*******"
    oauthswift.client.credential.oauth_token_secret = "*******"

    oauthswift.client.get("https://api.yelp.com/v2/search/...",
        success: {
            data, response in
            let dataString = NSString(data: data, encoding: NSUTF8StringEncoding)
            self.printYelpData(data)
            print(dataString)
        }
        , failure: { error in
            print(error)
        }
    )

我试着将requestTokenUrl等留空,但是它没有用,所以我只是输入了一个支持的URL,然后在初始化后设置它。

我希望有所帮助!