node-red / node-red-nodes

Extra nodes for Node-RED
Other
993 stars 594 forks source link

node-red-node-twitter API V1.1 problem to tweet #1000

Open ArfyFR opened 1 year ago

ArfyFR commented 1 year ago

Hi

Trying to send a tweet I get the error "You currently have access to Twitter API v2 endpoints and limited v1.1 endpoints only. If you need access to this endpoint, you may need a different access level. You can learn more here: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-leve"

According to https://twittercommunity.com/t/453-you-currently-have-essential-access-which-includes-access-to-twitter-api-v2-endpoints-only-if-you-need-access-to-this-endpoint-you-ll-need-to-apply-for-elevated-access-via-the-developer-portal-you-can-learn-more-here-https-developer-twitter-co/193001 it seems that API v1.1 can only be used to send Media now

To tweet: api V2

But in code the 1.1 seems to be used to tweet (with or without media) node.twitterConfig.post("https://api.twitter.com/1.1/statuses/update.json",{},params).then(function(result) { in node-red-node-twitter/27-twitter.js

ArfyFR commented 1 year ago

Sorry for disturbing. I found your names on https://flows.nodered.org/node/node-red-node-twitter @dceejay @knolleary do you know if there is a plan to correct it ?

b-neufeld commented 1 year ago

I don't have the skills to propose a fix but I managed to brute-force a workaround to keep Node-RED tweeting (sort of).

Here are some details I hope might help others patch up their tweeting applications.

First, I have Node-RED running in a Docker container. So, when the container is initialized I install tweepy with an exec block: image

The command it runs is curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python3 get-pip.py && export PATH="/usr/src/node-red/.local/bin/:$PATH" && pip install tweepy which grabs a .py installer script for pip, executes it, adds the pip directory to $PATH, and finally installs tweepy (via pip).

My tweeting flow looks like this: image

My Twitter bot tweets media that it grabs via the http request block. To get this to work, I can no longer pass a binary object to a Twitter block, I have to write it to a file with the Write File node. The Twitter Enable gate is logic that turns off tweets if data fails validation (not relevant to this workaround).

Finally the exec node calls cd / && cd data && python3 tweet_daily.py which executes tweet_daily.py in my data directory (which is mounted outside my docker container).

This is the tweepy script to tweet with media:

import tweepy
from tweepy import API, Client

# consumer key
# consumer secret
# access token
# access secret
auth = tweepy.OAuth1UserHandler(
   "asdf",
   "asdf",
   "asdf",
   "asdf"
)

client = tweepy.Client(
   consumer_key="asdf",
   consumer_secret="asdf",
   access_token="asdf",
   access_token_secret="asdf",
)

api = tweepy.API(auth)

# assume media is in same dir as python script
media = api.media_upload(filename="image_filename.png")
print("MEDIA: ", media)

tweet = client.create_tweet(text="Tweeting is fun and not super easy!", media_ids= 
[media.media_id_string])
print("TWEET: ", tweet)

And this is what it looks like to tweet without media:

import tweepy
from tweepy import API, Client

client = tweepy.Client(
   consumer_key="asdf",
   consumer_secret="asdf",
   access_token="asdf",
   access_token_secret="asdf",
)

tweet = client.create_tweet(text="Tweeting is fun and not super easy!")
print("TWEET: ", tweet)

Hope this helps someone else 👍

langfeld commented 1 year ago

Hope this helps someone else +1

Thank you very much, your detailed explanation has indeed helped me a lot :+1:

8666 commented 1 year ago

Is there are plan for a real fix? I can not tweet anything from my node-red bot