Tweepy API v2 "NoneType"

Viewed 318
import tweepy

client = tweepy.Client(consumer_key='REPLACE_ME',
                       consumer_secret='REPLACE_ME',
                       access_token='REPLACE_ME',
                       access_token_secret='REPLACE_ME')

# Replace the text with whatever you want to Tweet about
response = client.create_tweet(text='hello world')

print(response)

I am currently trying to work with this template code, but this is the error that I am currently getting:

TypeError: Consumer key must be string or bytes, not NoneType

I'm not sure how this counts as a NoneType error, the Consumer Key, Consumer Secret, Access Token, and Access Token Secret are all in quotations so they should be a string.

Thank you!

2 Answers

You can try something like this if you want to post a tweet.

import tweepy
consumerKey = 'REPLACE_ME'
consumerSecret = 'REPLACE_ME'
accessToken = 'REPLACE_ME'
accessTokenSecret = 'REPLACE_ME'

auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
auth.set_access_token(accessToken, accessTokenSecret)

api = tweepy.API(auth)

status = api.update_status(status="Hello world!")

I replicated your code on my machine (I am using Twitter API v2). I get an Unauthorized 401 error. The first argument to tweepy.Client() should be the bearer token, in addition to the 4 other keys of course.

After that, you may face Forbidden: 403 issues with Client.create_tweet() if you have not given your app write permissions. See how you can address this issue in the answer to this post.

Related