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

Erro ao Executar recent_search.py

Estou no segundo modulo e no segundo video e ao testar a classe "recent_search.py" me deparo com o seguinte erro.

401 Traceback (most recent call last): File "/home/adriano/datapipeline/recent_search.py", line 64, in main() File "/home/adriano/datapipeline/recent_search.py", line 59, in main for json_response in paginate(url, headers): File "/home/adriano/datapipeline/recent_search.py", line 50, in paginate data = connect_to_endpoint(full_url, headers) File "/home/adriano/datapipeline/recent_search.py", line 41, in connect_to_endpoint raise Exception(response.status_code, response.text) Exception: (401, '{"title":"Unauthorized","type":"about:blank","status":401,"detail":"Unauthorized"}')

Process finished with exit code 1

O meu código, está de acordo com o do exercício, segue abaixo:

import json
import os
import requests


# To set your enviornment variables in your terminal run the following line:
# export 'BEARER_TOKEN'='<your_bearer_token>'


def auth():
    return os.environ.get("BEARER_TOKEN")


def create_url():
    query = "AluraOnline"
    # Tweet fields are adjustable.
    # Options include:
    # attachments, author_id, context_annotations,
    # conversation_id, created_at, entities, geo, id,
    # in_reply_to_user_id, lang, non_public_metrics, organic_metrics,
    # possibly_sensitive, promoted_metrics, public_metrics, referenced_tweets,
    # source, text, and withheld
    tweet_fields = "tweet.fields=author_id,conversation_id,created_at,id,in_reply_to_user_id,public_metrics,text"
    user_fields = "expansions=author_id&user.fields=id,name,username,created_at"
    filters = "start_time=2021-02-15T00:00:00.00Z&end_time=2021-02-19T00:00:00.00Z"
    url = "https://api.twitter.com/2/tweets/search/recent?query={}&{}&{}&{}".format(
        query, tweet_fields, user_fields, filters
    )
    return url


def create_headers(bearer_token):
    headers = {"Authorization": "Bearer {}".format(bearer_token)}
    return headers


def connect_to_endpoint(url, headers):
    response = requests.request("GET", url, headers=headers)
    print(response.status_code)
    if response.status_code != 200:
        raise Exception(response.status_code, response.text)
    return response.json()


def paginate(url, headers, next_token=""):
    if next_token:
        full_url = f"{url}&next_token={next_token}"
    else:
        full_url = url
    data = connect_to_endpoint(full_url, headers)
    yield data
    if "next_token" in data.get("meta", {}):
        yield from paginate(url, headers, data['meta']['next_token'])

def main():
    bearer_token = auth()
    url = create_url()
    headers = create_headers(bearer_token)
    for json_response in paginate(url, headers):
        print(json.dumps(json_response, indent=4, sort_keys=True))


if _name_ == "_main_":
    main()
1 resposta
solução!

Ola Adriano, Acredito que esta faltando criar a variavel de ambiente do BEARER_TOKEN com a chave de acesso do Twitter API. Execute export BEARER_TOKEN=<chave_do_twitter> antes de executar o recent_search.py.