1

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

4

1 回答 1

0

你可以这样做:

    // 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 数据。

于 2016-02-07T03:58:45.430 回答