I’m getting Bad Authentication data
response in twitter friends/list API. I’m getting userid, screen name, authToken and authTokenSecret when login.
func loadFollowers(userid:String) {
//let twapi = "https://api.twitter.com/1.1/followers/list.json?cursor=-1&user_id=(session)&count=5000"
let twapi = "https://api.twitter.com/1.1/friends/list.json?cursor=-1&user_id=(userid)&count=10"
let url2 = URL(string: twapi)!
print(url2)
URLSession.shared.dataTask(with: url2, completionHandler: { (data, response, error) in
//UIApplication.shared.isNetworkActivityIndicatorVisible = false
do {
let userData = try JSONSerialization.jsonObject(with: data!, options:[])
print(userData)
} catch {
NSLog("Account Information could not be loaded (error)")
}
}).resume()
}
Output:
{
"errors": [
{
"code": 215,
"message": "Bad Authentication data."
}
]
}
What are the required parameters to send in friends/list.json
API.
In this document they given all parameters are optional.
https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-friends-list
2
Answers
In Swift 4.2, Xcode 10.1 and iOS 12.1
Finally i got the solution for this. Here first we need Authorisation then need to implement friends list api.
Pure Swift code is not available. But i implemented in pure swift.
If you want to get friends/list data from twitter you need to use two API's.
1) oauth2/token API
2) friends/list API
In oauth2/token api you can get access token, because you need access token for friends list. And you need user id, screen name.
But here you must remember one important point.
1) First use oauth2/token api for access token.
2) After getting access token use twitter login api for user id and screen name.
3) Now use friends/list api.
Here first if you use twitter login then oauth2/token api for access token, you can get like Bad Authentication data error. So you please follow above 3 steps in order.
1) Get access token code (oauth2/token api):
Output :
2) Login with twitter code
Output:
Here you will get userName, userId, authtoken, authTokenSecret, screen name and email etc.
3) Now get friends list from friends/list api. Here you can get friends/list, users/lookup, followers/ids, followers/list api's data etc...
This code not available any where. I tried a lot for this code and i spent lot of time for this. Thank you.
Because this friends/list api requires authentication in order to fetch friends list.