2016-02-15 75 views
0

我想用Yelp API使用OAuthSwift。其中一個必需的參數是requestTokenURL,authorizeURL,accessTokenURL;不過,我已經從Yelp獲得了令牌本身,並且Yelp沒有授權URL。不包括這些參數給我一個錯誤。使用Yelp API使用OAuthSwift的正確方法是什麼?謝謝。如何在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) 
    }) 
} 
+0

這個庫可能有助於在Swift中使用Yelp:https://github.com/dalequi/yelpitoff – Moritz

回答

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,然後在初始化之後設置。

我希望有幫助!