I am absolutely brand new to coding (about 11 days) and I was told to post my question here since I cannot solve my problem despite countless attempts.
I am using Python and Jupyter notebook (it is required that I use this)
I have a config file named config.txt where all my keys and tokens are located and I am able to call all my keys except the bearer_token which I do not know when to use this as I have seen hundreds of different answers.
I have a Twitter Academic Research account. My goal is to pull various hashtags, in order to do so I must be able to authenticate my account via my code (this is what I am understanding). I have tried various setup per different suggestions here on stackoverflow and I cannot authentic. I also get the following error message. Unauthorized: 401 Unauthorized 32 - Could not authenticate you. When I go to Twitter to learn and understand it says this:
401
Unauthorized
V1.1 V2 There was a problem authenticating your request. This could be due to missing or incorrect authentication credentials. This may also be returned in other undefined circumstances.
Check that you are using the correct authentication method and that your credentials are correct. The link provided no longer works and I don't know what I am doing to check my authentication methods and credentials are correct (I only have the one set of credentials so I am thinking that it is my authentication method...maybe??)
My code is further down.
This is my error message at the end of my last code block named authentication
I have tried multiple solutions by different people including all the recommendations mentioned in the [https://stackoverflow.com/questions/66156958/how-to-acess-tweets-with-bearer-token-using-tweepy-in-python/66597109#66597109]
import tweepy
import configparser
import os
#Define file path and make sure path is correct
file_name = "config.txt"
#Config file stored in the same directory as the script.
#Get correct working directory with os.getcwd()
file_path = os.path.join(os.getcwd(), file_name)
print(file_path) # Confirm file path is correct.
The path is correct and all is well for the above portion of code.
#Read info from the config file named config.txt
config = configparser.ConfigParser()
config.read(file_path)
#Will raise KeyError if the file path is not correct
api_key = config["twitter"]["API_key"]
api_secret = config["twitter"]["API_secret"]
access_token = config["twitter"]["Access_token"]
access_token_secret = config["twitter"]["Access_token_secret"]
#bearer_token = config["twitter"]["Bearer_token"]
#client_id = config["twitter"]["Client_ID"]
#client_secret = config["twitter"]["Client_Secret"]
#print(api_key, api_secret, access_token, access_token_secret)
#print (bearer_token)
Everything returns properly with the exception of my bearer_key - it is set up the same exact way as all my other keys and tokens in my config file
#authentication - this is where my issue is. My authentication is not working.
from tweepy.auth import OAuthHandler
auth = OAuthHandler("API_key", "API_secret")
auth.set_access_token = (access_token, access_token_secret)
api = tweepy.API(auth)
public_tweets = api.home_timeline()
print(public_tweets)
The following is just other example I have tried using of many to troubleshoot my issue
#auth = tweepy.OAuth2AppHandler("bearer_token", "API_key_secret")
#api = tweepy.API(auth)
#for tweet in tweepy.Cursor(api.search, q='cool').items(3):
#print(tweet.text)
No matter what, I get the error provided in the screenshot image above
What I really want to do is pull historical hashtags but this is not possible without authentication, I believe. It was recommended I try pulling my own timeline to start before I began writing queries for different pieces of data or information. I am sure there is something simplistic but being 11 days old in the world of coding I am quite lost. Thank you to everyone helping me to understand and troubleshoot. I really appreciate it.
CodePudding user response:
You are not passing your api_key and api_secret.
This:
auth = OAuthHandler("API_key", "API_secret")
should be this:
auth = OAuthHandler(api_key, api_secret)