2
respostas

Error 401 - Unauthorized

Olá, poderiam me ajudar? estou fazendo tudo certo, e até mesmo gerei uma nova chave para declarar o export bearer token, mas mesmo assim continua dando erro. Poderiam me ajudar?

Segue o código

import requests
import os
import json

# 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 = "from:twitterdev -is:retweet"
    query = 'atypocalipse'

    # 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-08-10T00:00:00.00Z&end_time=2021-08-15T00:00:00.00Z" 
    #periodo inicial e o final, apenas ate 7 dias de periodo
    url = f"https://api.twitter.com/2/tweets/search/recent?query={query}&{tweet_fields}&{user_fields}&{filters}"

    return url


def create_headers(bearer_token):
    headers = {f"Authorization": "Bearer {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 main():
    bearer_token = auth()
    url = create_url()
    headers = create_headers(bearer_token)
    json_response = connect_to_endpoint(url, headers)
    print(json.dumps(json_response, indent=4, sort_keys=True))


if __name__ == "__main__":
    main()
2 respostas

Estou com o mesmo problema. Estou executando o arquivo recent_search.py no mesmo terminal que declarei a BEARER_TOKEN e mesmo assim recebo o Erro 401

estou com o mesmo erro 401 mas estou rodando o twitter_hook. Alguem pode me ajudar? Erro