jdoe
jdoe

Reputation: 79

Tweepy + api v1 + keys

I have python code that posts tweets to twitter using oauth2 and that works fine but I need to attach some images to tweets and it seems that has to be done with api v1.1

On the twitter dev portal under project settings, user authentication it says "oAuth 1.0a and OAuth 2.0 turned on" and the Oauth 1.0a settings app permissions are read and write. I'm confused about which keys I should be using for Oauth 1.0a as when I use the consumer keys like this

my_api_key="xxx"
my_api_secret="xxx"

auth = tw.OAuthHandler(my_api_key, my_api_secret)
api = tw.API(auth, wait_on_rate_limit=True)

tweet = api.update_status("Testing tweepy API v1")

I get the error 220 - Your credentials do not allow access to this resource.

If I copy the access keys from the OAuth2 code that works then I get this error 32 - Could not authenticate you.

The OAuth2 code that works looks like this:

api = tweepy.Client(bearer_token=keys.twitter_bearer,
                    access_token=keys.access_key,
                    access_token_secret=keys.access_secret,
                    consumer_key=keys.consumer_key,
                    consumer_secret=keys.consumer_secret_key)

api.create_tweet(text=str)

I'm not sure what I'm doing wrong.

edit - am elevated

Upvotes: 0

Views: 902

Answers (1)

jdoe
jdoe

Reputation: 79

Went back to basics and investigated the code as everything else sounds right. Turns out I was just doing it wrong.

This works:

auth = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret, access_token, access_token_secret
)

api = tweepy.API(auth)
image=".\dataframe.png"
media=api.media_upload(image)

try:
   api.update_status("Test",media_ids=[media.media_id])
except Exception as e:
   print(e.class)
   print(f"Exception occured - {e}")
else:
   print("Success")

Upvotes: 1

Related Questions