I have the basic paid plan of Twitter. I am trying to obtain users information using this code:
import requestsimport jsonimport configbearer_token = config.bearer_tokenurl = "https://api.twitter.com/2/users"params = {'ids': '[4919451, 14151086]','user.fields':'name,description,location,public_metrics,username,created_at,' }def bearer_oauth(r):""" Method required by bearer token authentication.""" r.headers["Authorization"] = f"Bearer {bearer_token}" r.headers["User-Agent"] = "v2UserLookupPython" return rdef connect_to_endpoint(url,params): response = requests.request("GET", url, auth=bearer_oauth, params=params) print(response.status_code) if response.status_code != 200: raise Exception("Request returned an error: {} {}".format( response.status_code, response.text ) ) return response.json()def main(): url = create_url() json_response = connect_to_endpoint(url,params) json_response = flatten(json_response) d = json.dumps(json_response, indent=4, sort_keys=True) return dif __name__ == "__main__": main()
I am getting this error:
Exception: Request returned an error: 400 {"errors":[{"parameters":{"ids":["4919451, 14151086"]},"message":"The `ids` query parameter value [ 14151086] is not valid"},{"parameters":{"user.fields":["name,description,location,public_metrics,username,created_at,"]},"message":"The `user.fields` query parameter value [] is not one of [connection_status,created_at,description,entities,id,location,most_recent_tweet_id,name,pinned_tweet_id,profile_image_url,protected,public_metrics,receives_your_dm,subscription_type,url,username,verified,verified_type,withheld]"}],"title":"Invalid Request","detail":"One or more parameters to your request was invalid.","type":"https://api.twitter.com/2/problems/invalid-request"}
Can you help me to improve the function connect_to_endpoint so it works?Appreciate your answers in advance.