skip to Main Content

I am trying to block a user account with the Twitter API through Python. Here’s what my method looks like:

def block_user():

payload = {"target_user_id": "amazon"}

# Get request token
request_token_url = "https://api.twitter.com/oauth/request_token"
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)

# 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(
    consumer_key,
    client_secret=consumer_secret,
    resource_owner_key=resource_owner_key,
    resource_owner_secret=resource_owner_secret,
    verifier=verifier,
    # I tried this, it didn't work
    #oauth_callback='oob',
)
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(
    consumer_key,
    client_secret=consumer_secret,
    resource_owner_key=access_token,
    resource_owner_secret=access_token_secret,
)

# Making the request
response = oauth.post(
    "https://api.twitter.com/2/users/{}/blocking".format(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))

# Saving the response as JSON
json_response = response.json()
print(json.dumps(json_response, indent=4, sort_keys=True))

In the terminal when I run the script, I get this:

Got OAuth token: 9SL...BGo
Please go here and authorize: https://api.twitter.com/oauth/authorize?oauth_token=9SL...BGo
Paste the PIN here: 

So I go to the URL and it asks me to authorize the app. I authorize it, and then get redirected immediately to the redirect page of the Twitter API app settings (default.com in this case). I don’t get shown a PIN to put into the prompt, I don’t see any success message or anything, it just redirects immediately. Can’t find anyone else with that issue using traditional search terms.

Anyone got any ideas?

2

Answers


  1. Chosen as BEST ANSWER

    I found the answer myself. So you need to add the following URL parameters to your auth/request_token URL. For me, that meant changing this line (the third line in my code block):

    request_token_url = "https://api.twitter.com/oauth/request_token"
    

    to this:

    request_token_url = "https://api.twitter.com/oauth/request_token?oauth_callback=oob&x_auth_access_type=write"
    

    I had tried using oauth_callback=oob in a few other places including my OAuth1Session object and on the authentication URL. Needs to be on the request_token URL.


  2. sorry i can’t comment right now so i will type this here :

    i really recommend using the Twitter library :

    Twitter python library

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search