21
loading...
This website collects cookies to deliver better user experience
Create New Project
and fill in the required information for the project and the App creation. After filling out the entire form, you will see a screen with the authentication keys of your application; they will be necessary when connecting to the API.from decouple import config
import requests
import json
.env
, and inside it, we will add the following information.BEARERTOKEN=BearerTokenValue
.py
file.bearer_token = config('BEARERTOKEN')
bearer_token
variable will now hold the value of your Bearer Token.def set_auth(ob_auth):
objeto_para_auth.headers["Authorization"] = f"Bearer {bearer_token}"
return ob_auth
search_url = "https://api.twitter.com/2/tweets/search/recent"
str_busca = f'to:{profile} {keyword} lang:{language}'
query_params = {'query': str_aux,'tweet.fields': 'attachments,author_id,created_at,lang,public_metrics,source','max_results':limit_tweets}
search_url
variable receives the endpoint that will be used (you can read a little more about the endpoints available by clicking here). Furthermore, the str_busca variable receives the search string in the case of the example the search will be for tweets sent to a profile, containing a search key and written in a specific language, you can found other search filters here. Lastly, the query-params receives the search string, the fields that will be present in the responses (see more options by clicking here) and the number of tweets returned in the search.response = requests.get(search_url, auth=set_autenticacao, params=query_params)
if response.status_code != 200:
raise Exception(response.status_code, response.text)
r = response.json()
print(json.dumps(r, indent=4, sort_keys=True))