Update twitter posting api to use v2
authorMagnus Hagander <magnus@hagander.net>
Mon, 26 Jun 2023 09:21:00 +0000 (11:21 +0200)
committerMagnus Hagander <magnus@hagander.net>
Mon, 26 Jun 2023 09:21:00 +0000 (11:21 +0200)
Only the v2 API is free now. Let's hope the fix is to just switch to
that version, and possibly re-generate tokens (this has worked for other
systems).

pgweb/news/management/commands/twitter_post.py
pgweb/news/management/commands/twitter_register.py

index 6ceb7d7876dda2f044eb31a1179bc2633d1ea37d..4e2e1a85de23e40b57e345c3548220a73d23d43e 100644 (file)
@@ -39,10 +39,10 @@ class Command(BaseCommand):
         for a in articles:
             # We hardcode 30 chars for the URL shortener. And then 10 to cover the intro and spacing.
             statusstr = "News: {0} {1}/about/news/{2}-{3}/".format(a.title[:140 - 40], settings.SITE_ROOT, slugify(a.title), a.id)
-            r = tw.post('https://api.twitter.com/1.1/statuses/update.json', data={
-                'status': statusstr,
+            r = tw.post('https://api.twitter.com/2/tweets', data={
+                'text': statusstr,
             })
-            if r.status_code != 200:
+            if r.status_code != 201:
                 print("Failed to post to twitter: %s " % r)
             else:
                 a.tweeted = True
index 319898f80f7f37ceb37936e1c3417292a387f370..96c9710e21efe1f1c010003b1139bd0892daa6c4 100644 (file)
@@ -24,7 +24,7 @@ class Command(BaseCommand):
             raise CommandError("TWITTER_TOKENSECRET is already set in settings_local.py")
 
         # OK, now we're good to go :)
-        oauth = requests_oauthlib.OAuth1Session(settings.TWITTER_CLIENT, settings.TWITTER_CLIENTSECRET)
+        oauth = requests_oauthlib.OAuth1Session(settings.TWITTER_CLIENT, settings.TWITTER_CLIENTSECRET, callback_uri='oob')
         fetch_response = oauth.fetch_request_token('https://api.twitter.com/oauth/request_token')
 
         authorization_url = oauth.authorization_url('https://api.twitter.com/oauth/authorize')