Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 tasks
ever3001 opened this issue Jul 23, 2020 · 8 comments · May be fixed by #669
Open
5 tasks

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

ever3001 opened this issue Jul 23, 2020 · 8 comments · May be fixed by #669

Comments

@ever3001
Copy link

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)
@PlkMarudny
Copy link

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

@ever3001
Copy link
Author

ever3001 commented Jul 24, 2020

Oh you are right. Now it works somehow. Thank you! 👍

@johnmckerrell
Copy link

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?

@knolleary
Copy link
Member

@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.

@johnmckerrell
Copy link

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
Copy link

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

@johnmckerrell johnmckerrell linked a pull request Aug 6, 2020 that will close this issue
6 tasks
@ever3001
Copy link
Author

ever3001 commented Aug 6, 2020

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 ever3001 reopened this Aug 6, 2020
@ajlennon
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants