0

I'm trying to create a tweet with a bot using tweepy, but I keep getting errors (as shown below).

I'm using Python 3 and Twitter APIv2.

When running only the api.verify_credentials, the authentication does succeed.

UPDATE

I managed to run this code successfully:

import tweepy

client = tweepy.Client(
    consumer_key="ISNvpSxvQlNYXkclzp6Q3Egwv",
    consumer_secret="Y1kf2kYEseRntlmaE361k5qdjAVeHYz7j8vFcrXVcmdZqAlkYV",
    access_token="1550947094185349127-zB6X2W0FshN2tibAoCDAroVPtEYy8X",
    access_token_secret="5qnC5DfJmC7UEHgw8L6eTPtzCYnxh8CvWLQ2wvecNzx4x"
)

filename=open('content.txt.txt')

f=filename.readlines()

filename.close()

for line in f:
 try:
  client.create_tweet(text=line)
  print(line)
 except tweepy.errors.TweepyException as err:
   print("err")
   
print("יא גאון מחשבים")
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Jul 30 '23 at 09:06
  • i wrote what errors i get, but i will make it clear in the body of the question – Toma Schreibman Jul 30 '23 at 10:47
  • This seems to have little to do with text files, is the error not the same if you use a fixed string instead, like `tweepy.Client.create_tweet(Self, text = "hello", user_auth=True)`? – mkrieger1 Jul 30 '23 at 11:04
  • Are you sure that you do not need to create an *instance* of `tweepy.Client` first, and *then* call its `create_tweet` method? – mkrieger1 Jul 30 '23 at 11:08
  • when changing to fixed I get these errors: Traceback (most recent call last): line 16, in if api.verify_credentials() == False: ^^^^^^^^^^^^^^^^^^^^^^^^ line 46, in wrapper return method(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^ line 2492, in verify_credentials return self.request( ^^^^^^^^^^^^^ line 269, in request raise Unauthorized(resp) tweepy.errors.Unauthorized: 401 Unauthorized 89 - Invalid or expired token. – Toma Schreibman Jul 30 '23 at 11:09
  • That's a different error from *before* `create_tweet` is even called. Here, `verify_credentials` failed. – mkrieger1 Jul 30 '23 at 11:11
  • update: when changing to fixed script i'm getting the same errors (edited the code in the question) – Toma Schreibman Jul 30 '23 at 11:16

1 Answers1

0

create_tweet is an instance method of the Client (or more correct the BaseClient) class. calling it like a static method and passing typing.Self to it as the instance is nothing that could ever work (you could pass api as the instance and it would work, but Python can pass the self parameter implicitly when you use something like api.create_tweet(...)).

Some further reading on everything:

Clasherkasten
  • 488
  • 3
  • 9
  • so should i just change 'tweepy.Client.create_tweet(Self, text = line, user_auth=True)' to api.create_tweet(Self, text = line, user_auth=True)? or removing the Self? – Toma Schreibman Jul 30 '23 at 11:38
  • change it to `api.create_tweet(text=line, user_auth=True)` (notice the change to `api.create_tweet(...)` and the removal of `Self`) – Clasherkasten Jul 30 '23 at 14:01