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

后端 未结 3 1314
鱼传尺愫
鱼传尺愫 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<String,Any>
                        print(response)
                        // print((response["statuses"] as! Array<Any>).count)
    
                    } catch let error as NSError {
                        print(error)
                    }
                }
    
                task.resume()
    
            } else {
                print(error?.localizedDescription as Any)
            }
        }
    
    }
    
    0 讨论(0)
  • 2021-02-12 18:14

    While there is no direct API to get the trendline, getting the followers count is fairly easy, access via the URL:

    http://api.twitter.com/1/users/show.json?user_id=12345
    

    The documentation has it all @ https://dev.twitter.com/docs/api/1/get/users/show

    To get the trendline, seems like I will need to query it on a daily basis!

    Updated to Twitter API v1.1

    https://api.twitter.com/1.1/users/show.json?user_id=12345
    

    Documentation at https://dev.twitter.com/docs/api/1.1/get/users/show

    Updated 31-May-2018

    The new API end point is at

    https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-users-show
    
    0 讨论(0)
  • 2021-02-12 18:23

    Here is a simple PHP exemple using CURL, with no library involved, to get the followers_count of a selected profile (here @TwitterFrance) using v2 API and the bearer token (bearer token is some kind of simplified method to access public data through an OAuth 2.0 API)

    $authorization = "Authorization: Bearer YOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEAR";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, "https://api.twitter.com/2/users/by/username/TwitterFrance?user.fields=public_metrics");
    $result = curl_exec($ch);
    curl_close($ch);
    if (is_string($result)) {
        echo (json_decode($result)->data->public_metrics->followers_count);
        die();
    }
    
    0 讨论(0)
提交回复
热议问题