Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Erro acesso API 502 Bad Gateway

Código

from datetime import datetime, timedelta
import requests
import os

TIMESTAMP_FORMAT = "%Y-%m-%dT%H:%M:%S.00Z"
end_time = datetime.now()
start_time = (end_time + timedelta(-1)).date().strftime(TIMESTAMP_FORMAT)

query = "data science"

tweet_fields = "tweet.fields=author_id,conversation_id,created_at,id,in_reply_to_user_id,public_metrics,lang,text"
user_fields = "expansions=author_id&user.fields=id,name,username,created_at"

url_raw = f"https://labdados.com/2/tweets/search/recent?query={query}&{tweet_fields}&{user_fields}&start_time={start_time}&end_time={end_time}"

bearer_token = os.environ.get("BEARER_TOKEN")
headers = {
    "Authorization": "Bearer {}".format(bearer_token),
}
response = requests.request("GET", url_raw, headers=headers)
print(response.text)

Retorno

<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.18.0 (Ubuntu)</center>
</body>
</html>
1 resposta
solução!

Algo do código estava errado. Abaixo código correto funcionando

from datetime import datetime, timedelta
import os
import requests
import json


TIMESTAMP_FORMAT = "%Y-%m-%dT%H:%M:%S.00Z"

end_time = datetime.now().strftime(TIMESTAMP_FORMAT)
start_time = (datetime.now() + timedelta(-1)).date().strftime(TIMESTAMP_FORMAT)

query = "data science"

tweet_fields = "tweet.fields=author_id,conversation_id,created_at,id,in_reply_to_user_id,public_metrics,lang,text"
user_fields = "expansions=author_id&user.fields=id,name,username,created_at"

url_raw = f"https://labdados.com/2/tweets/search/recent?query={query}&{tweet_fields}&{user_fields}&start_time={start_time}&end_time={end_time}"

bearer_token = os.environ.get("BEARER_TOKEN")
headers = {"Authorization": "Bearer {}".format(bearer_token)}
response = requests.request("GET", url_raw, headers=headers)


response = requests.request("GET", url_raw)

json_response = response.json()

print(json.dumps(json_response, indent=4, sort_keys=True))