Hi,
Thanks for the follow-up!
I followed your code example verbatim:
from requests_oauthlib import OAuth1Session
my_consumer_key = ""
my_consumer_secret = ""
user_id = "" # my user_id, which matches the first part of the access_token
payload = {"target_user_id": "15772978"} # Jessica's user_id
# Get request token
request_token_url = "https://api.twitter.com/oauth/request_token"
oauth = OAuth1Session(my_consumer_key, client_secret=my_consumer_secret)
fetch_response = oauth.fetch_request_token(request_token_url)
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)
# Get authorization
base_authorization_url = "https://api.twitter.com/oauth/authorize"
authorization_url = oauth.authorization_url(base_authorization_url)
print("Please go here and authorize: %s" % authorization_url)
verifier = input("Paste the PIN here: ")
# Get the access token
access_token_url = "https://api.twitter.com/oauth/access_token"
oauth = OAuth1Session(
my_consumer_key,
client_secret=my_consumer_secret,
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret,
verifier=verifier,
)
oauth_tokens = oauth.fetch_access_token(access_token_url)
access_token = oauth_tokens["oauth_token"]
access_token_secret = oauth_tokens["oauth_token_secret"]
# Make the request
oauth = OAuth1Session(
my_consumer_key,
client_secret=my_consumer_secret,
resource_owner_key=access_token,
resource_owner_secret=access_token_secret,
)
assert access_token.split("-")[0] == str(user_id)
# Making the request
response = oauth.post(
"https://api.twitter.com/2/users/{}/following".format(user_id), json=payload
)
if response.status_code != 200:
raise Exception(
"Request returned an error: {} {}".format(response.status_code, response.text)
)
print("Response code: {}".format(response.status_code))
I also added an assert statement, which validates that the first part of the access_token matches my user_id. But the error message is still the same:
Exception: Request returned an error: 401 {"title":"Unauthorized","type":"about:blank","status":401,"detail":"Unauthorized"}
I wish the error message was more useful 
To answer your questions specifically:
Have you tried regenerating the tokens?
- Yes, and I added the assert statement to validate the access_token
Have been able to make a request to other v2 endpoints with the same conditionals?
- Yes, I made a search request using these keys which worked correctly. Unfortunately most of the requests in the v2 API require the bearer token, not a user-context. But search worked correctly:
response = oauth.get("https://api.twitter.com/2/tweets/search/recent?query=from%3Atwitterdev%20new%20-is%3Aretweet&max_results=10")
assert response.status_code == 200
You may want to double-check, you have properly set up your Twitter app, then chances are that you aren’t handling number two properly. If so, please consider using an oauth library (example ), using Insomnia, or try using Twurl.
- I am using requests_oauthlib and not sure what you mean by "handling number two properly*.
Thanks!