'resource_owner_key = fetch_response.get("oauth_token") NameError: name 'fetch_response' is not defined in tweepy [closed]
I'm trying to make a Twitter bot and my code is getting like this:
from enum import auto
from logging import raiseExceptions
from cv2 import detail_AffineBasedEstimator
from requests_oauthlib import OAuth1Session
import os
import json
consumer_key = os.environ.get("xxxxxxxxx")
consumer_secret = os.environ.get("xxxxxxxxxxxxxxxx")
payload = {"text": "LEZZZZ GOOOOO"}
request_token_url = "https://api.twitter.com/oauth/request_token?oauth_callback=oob&x_auth_access_type=write"
oauth = OAuth1Session(consumer_key, client_secret=consumer_secret)
try:
fetch_response = oauth.fetch_request_token(request_token_url)
except ValueError:
print(
"There may have been an issue with the consumer_key or consumer_secret you entered."
)
resource_owner_key = fetch_response.get("oauth_token")
resource_owner_secret = fetch_response.get("oauth_token_secret")
print("Got OAuth token: %s" % resource_owner_key)
base_authorization_url = "https://api.twitter.com/oauth/authorize"
authorization_url = oauth.authorization_url(base_authorization_url)
print("Vem aqui autorizar, vem: %s" % authorization_url)
verifier = input("Cole o PIN aqui: ")
acess_token_url = "https://api.twitter.com/oauth/access_token"
oauth = OAuth1Session(
consumer_key,
client_secret=consumer_secret,
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret,
verifier=verifier,
)
oauth_tokens = oauth.fetch_access_token(acess_token_url)
access_token = oauth_tokens["oauth_token"]
access_token_secret = oauth_tokens["oauth_token_secret"]
oauth = OAuth1Session(
consumer_key,
client_secret=consumer_secret,
resource_owner_key=access_token,
resource_owner_secret=access_token_secret,
)
response = oauth.post(
"https://api.twitter.com/2/tweets",
json = payload
)
if response.status_code != 201:
raise Exception(
"Request returned an error: {} {}".format(response.status_code,response.text)
)
print ("Response code: {}".format(response.status_code))
json_response = response.json()
print(json.dumps(json_response, indent=4, sort_keys = True))
But when I try to run it i get this error: There may have been an issue with the consumer_key or consumer_secret you entered.
Traceback (most recent call last):
File "c:\Users\user\Desktop\Twitter Bot\twitter.py", line 24, in <module>
resource_owner_key = fetch_response.get("oauth_token")
NameError: name 'fetch_response' is not detail_AffineBasedEstimator
I have already refreshed my keys and it's still not working!
Solution 1:[1]
First of all, you should refresh your keys if you just posted the most recent version of them.
Then, I am not sure to understand those two lines:
consumer_key = os.environ.get("xxxxxxxxxxxxxxxx")
consumer_secret = os.environ.get("xxxxxxxxxxxxxxxxxxxxxxxxxxxx")
Either you set the keys in the environ dict and you can retrieve them this way:
consumer_key = os.environ.get("the_name_you_give_to_the_consumer_key")
consumer_secret = os.environ.get("the_name_you_give_to_the_consumer_secret")
Either you directly set the keys to the variables:
consumer_key = "xxxxxxxxxxxxxxxx"
consumer_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
It does not make sense to mix the two.
Then, as the other answer mentioned, you are silencing an important error:
try:
fetch_response = oauth.fetch_request_token(request_token_url)
except ValueError:
print(
"There may have been an issue with the consumer_key or consumer_secret you entered."
)
You are handling the error so the code continue to run after that... but without the value of fetch_response. So it obviously leads to some errors later in your code when you try to use this value.
Finally, I would strongly advise you to use some library like Tweepy.
It can take care of most of the authentication process for you (see here).
And if you can't use it, you can at least read how they do this authentication here.
Solution 2:[2]
Your code is silencing the ValueError that is happening before line 24. Let the error explode to understand it.
Because it throws the exception, your resource_owner_key is not defined.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mickael Martinez |
| Solution 2 | iurisilvio |
