【问题标题】:How to get foursquare user information after getting auth token in iOS using swift使用swift在iOS中获取身份验证令牌后如何获取foursquare用户信息
【发布时间】:2016-02-05 11:09:30
【问题描述】:

我正在尝试将foursquare api集成到应用程序中,成功登录并获得authtoken。现在我需要使用 authtoken 获取登录用户信息,如何获取用户信息(如用户名和 emailId 等)。请帮帮我。

【问题讨论】:

    标签: swift2 foursquare xcode7.1 ios9.2


    【解决方案1】:

    你可以这样做:

        // Construct url object via string
        let url = NSURL(string: "https://api.foursquare.com/v2/users/self?oauth_token=(YOUR_OAUTH_TOKEN)&v=20160207")
        let config = NSURLSessionConfiguration.defaultSessionConfiguration()
        let session = NSURLSession(configuration: config)
        let req = NSURLRequest(URL: url!)
    
        // NSURLSessionDownloadTask is retured from session.dataTaskWithRequest
        let task = session.dataTaskWithRequest(req, completionHandler: {
            (data, resp, err) in
            print(NSString(data: data!, encoding: NSUTF8StringEncoding))
    
            do {
                let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
    
                if let response = json["response"] as? [String: AnyObject] {
                    if let user = response["user"] as? [String: AnyObject] {
                        if let name = user["firstName"] as? String {
                            // User name
                            print(name)
                        }
                    }
                }
            } catch {
                print("error serializing JSON: \(error)")
            }
    
        })
        task.resume()
    

    如果使用https://github.com/SwiftyJSON/SwiftyJSON,可以轻松处理JSON数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-25
      • 2011-10-06
      • 1970-01-01
      • 2021-06-06
      相关资源
      最近更新 更多