node-red-nodes icon indicating copy to clipboard operation
node-red-nodes copied to clipboard

node-red-node-twitter error by posting a tweet

Open ever3001 opened this issue 4 years ago • 8 comments

Which node are you reporting an issue on?

Node-Red-Node-Twitter

What are the steps to reproduce?

  • Open an node red project
  • Install Node-Red-Node-Twitter
  • Insert a node twitter out
  • Configure credentials
  • Add a simple inject node with msg.payload = "Hello world" and msg.topic = ""
  • Connect inject node to twitter node
  • Deploy
  • Inject information to twitter node

What happens?

A message "TypeError: Cannot read property '0' of undefined" displays in debug tab

What do you expect to happen?

Tweet Hello world in the account configured

Please tell us about your environment:

  • [ ] Node-RED version: v1.1.2
  • [ ] node.js version: v12.18.2
  • [ ] npm version: 6.14.6
  • [ ] Platform/OS: MX-19.2_x64 patito feo May 31 2020
  • [ ] Browser: Version 83.0.4103.116 (Official Build) (64-bit)

ever3001 avatar Jul 23 '20 15:07 ever3001

I observed the same, but suddenly everything went back to normal. Guessing this was a Twitter glitch somehow.

PlkMarudny avatar Jul 24 '20 06:07 PlkMarudny

Oh you are right. Now it works somehow. Thank you! :+1:

ever3001 avatar Jul 24 '20 09:07 ever3001

I am currently getting this, the stack reports:

TypeError: Cannot read property '0' of undefined
    at /home/pi/.node-red/node_modules/node-red-node-twitter/27-twitter.js:645:66
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

That line of code is:

node.error(result.body.errors[0].message,msg);

I dumped the result body and it was showing:

{
  request: '/1.1/statuses/update.json',
  error: 'Read-only application cannot POST.'
}

I just checked on Twitter's documentation though and they claim the error should be compatible with the existing code.

Having the node's code so dependent on Twitter's API response structure could be considered a bad thing, but then knowing what the fallback should be is tricky. I've just tried the following which is quite "belt and braces" but might be considered overkill?

if (result.body.errors && result.body.errors[0] && result.body.errors[0].message) {
  node.error(result.body.errors[0].message,msg);
} else if (result.body.error) {
  node.error(result.body.error,msg);
} else {
  node.error("Twitter gave status: "+result.status+", unexpected body: "+JSON.stringify(result.body),msg);
}

So in my circumstance the second if clause is triggering, but if that failed in the future it would fallback on just dumping out the entire body and reporting the status.

Any thoughts?

johnmckerrell avatar Aug 06 '20 11:08 johnmckerrell

@johnmckerrell adding better error handling would be welcome.

The error you're hitting is because the credentials you are using don't have permission to post to the stream. I imagine that could be a common mistake made when setting up the credentials - so making sure the node reports back properly would be useful.

If its something you wanted to PR, I happily take a look.

knolleary avatar Aug 06 '20 11:08 knolleary

Thanks @knolleary yeah as soon as I actually had the error message I fixed the problem on my side. If the code I posted above seems reasonable I'll be happy to PR it sometime, first time committing so wasn't sure whether that code would make sense but I guess the ideal place to discuss it is on a PR!

johnmckerrell avatar Aug 06 '20 11:08 johnmckerrell

(Worth reopening this issue until the fix is out? I don't appear to have privileges)

johnmckerrell avatar Aug 06 '20 12:08 johnmckerrell

The error is in Twitter App permissions. When you generate an app in Twitter, the default permissions are only Read (Read Tweets and profile information), so you need to change it for "Read and Write" or "Read + Write + Direct Messages". To change the permissions you need:

  1. Go to your dashboard (https://developer.twitter.com/en/portal/dashboard).
  2. Click on your app settings.
  3. Click on the button "edit" in section "App permissions".
  4. Change the permissions to "Read and Write" or "Read + Write + Direct Messages".

After that you have to regenerate the Access Token & Secret Token to have the same permissions as the application:

  1. Go to your dashboard (https://developer.twitter.com/en/portal/dashboard).
  2. Click in Keys and tokens.
  3. Click in the button "Regenerate" in section "Authentication Tokens".

After that you have to reconfigure the node in node-red with the new tokens and keys and now should work. That's how I find the solution to the problem. So for the error improvement, I will open the Issue again.

ever3001 avatar Aug 06 '20 13:08 ever3001

I love it when I'm trying to solve a problem and I find @johnmckerrell was there before me ;)

ajlennon avatar Oct 21 '21 12:10 ajlennon