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
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)
}
}
}
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
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();
}