How to get a count of followers from Twitter API and trendline

后端 未结 3 1422
鱼传尺愫
鱼传尺愫 2021-02-12 17:28

I am in the process of writing some reports for the number of followers over time for Twitter, however after substantial searches and trial and error, I have not being able to g

3条回答
  •  终归单人心
    2021-02-12 18:13

    In Swift 4.2 and Xcode 10.1 to get twitter followers_count

    Here you need to integrate twitter SDK into you app and follow integration details https://github.com/twitter/twitter-kit-ios

    //This is complete url 
    https://api.twitter.com/1.1/users/show.json?screen_name=screenName
    
    func getStatusesUserTimeline(accessToken:String) {
    
        let userId = "109*************6"
        let twitterClient = TWTRAPIClient(userID: userId)
        twitterClient.loadUser(withID: userId) { (user, error) in
            print(userId)
            print(user ?? "Empty user")
            if user != nil {
                var request = URLRequest(url: URL(string: "https://api.twitter.com/1.1/users/show.json?screen_name=screenName")!)
    
                request.httpMethod = "GET"
                request.setValue("Bearer "+accessToken, forHTTPHeaderField: "Authorization")
                print(request)
    
                let task = URLSession.shared.dataTask(with: request) { data, response, error in guard let data = data, error == nil else { // check for fundamental networking error
                    print("error=\(String(describing: error))")
                    return
                    }
    
                    if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
                        print("statusCode should be 200, but is \(httpStatus.statusCode)")
                        print("response = \(String(describing: response))")
                    }
    
                    do {
                        let response = try JSONSerialization.jsonObject(with: data, options: []) as! Dictionary
                        print(response)
                        // print((response["statuses"] as! Array).count)
    
                    } catch let error as NSError {
                        print(error)
                    }
                }
    
                task.resume()
    
            } else {
                print(error?.localizedDescription as Any)
            }
        }
    
    }
    

提交回复
热议问题