super-ienien / twitter-webhooks

Easy to use expressJs middleware for twitter webhooks api
MIT License
20 stars 3 forks source link

User access token #5

Closed nichis closed 5 years ago

nichis commented 5 years ago

Where can I find the following parameters?

//Subscribe for a particular user activity userActivityWebhook.subscribe({ userId: '[TWITTER USER ID]', accessToken: '[TWITTER USER ACCESS TOKEN]', accessTokenSecret: '[TWITTER USER ACCESS TOKEN SECRET]' })

I can find only the following parameters: accessToken: '[YOUR APP ACCESS TOKEN]', accessTokenSecret: '[YOUR APP ACCESS TOKEN SECRET]',

I'm having the following errors: Error: Unable to connect during CRC GET request. Error: Webhook URL must be defined to create subscriptions.

can be related to access token error? My webhook works since if I test it using: https://MYDOMAIN/?crc_token=123456 I get the right response.

super-ienien commented 5 years ago

Hi,

did you provide your server's URL when you create the userActivity webhook ? you need to set the serverUrl parameter on the webhook's config object.

const userActivityWebhook = twitterWebhooks.userActivity({
    serverUrl: 'https://yourdomain.com',
    consumerKey: '[YOUR CONSUMER KEY]',
    consumerSecret: '[YOUR CONSUMER SECRET]',
    accessToken: '[YOUR APP ACCESS TOKEN]',
    accessTokenSecret: '[YOUR APP ACCESS TOKEN SECRET]',
    environment: '[your-env]', //default : 'env-beta'
    app
});
nichis commented 5 years ago

Yes I do, even if i was using a different port instead of 443. Now using the 443 port I get the following errors:

(node:19975) UnhandledPromiseRejectionWarning: Error: Webhook URL does not meet the requirements. Please, check your SSL Configuration. at Request.request [as _callback] (/home/laiconbots/twitterApp/node_modules/twitter-webhooks/lib/helpers.js:27:33) at Request.self.callback (/home/laiconbots/twitterApp/node_modules/request/request.js:185:22) at Request.emit (events.js:182:13) at Request.<anonymous> (/home/laiconbots/twitterApp/node_modules/request/request.js:1161:10) at Request.emit (events.js:182:13) at IncomingMessage.<anonymous> (/home/laiconbots/twitterApp/node_modules/request/request.js:1083:12) at Object.onceWrapper (events.js:273:13) at IncomingMessage.emit (events.js:187:15) at endReadableNT (_stream_readable.js:1081:12) at process._tickCallback (internal/process/next_tick.js:63:19) (node:19975) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:19975) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. Promise Rejected: {"code":170,"statusCode":403,"body":{"errors":[{"code":170,"message":"Webhook URL must be defined to create subscriptions."}]}}

My SSL configurations are:

`const server = https.createServer({ key: fs.readFileSync('./key.pem'), cert: fs.readFileSync('./cert.pem'), passphrase: 'MY_PASSPHRASE', hostname: 'https://MY_IP', port: 443, path: '/', method: 'GET' },app);

server.listen(443);`

and always when I go with the browser to https://MY_DOMAIN_IP/?crc_token=123456 I get the following response: {"response_token":"sha256=kinV2YMpuOx5F15Ae8l+h69VlTrkY6DdNHXs+em3sjc="}

super-ienien commented 5 years ago

Sorry, I think I need to see your code to understand what's wrong.

nichis commented 5 years ago

Here is the code:

` const express = require ('express'); const bodyParser = require ('body-parser'); const twitterWebhooks = require('twitter-webhooks'); const https = require ('https'); const fs = require('fs');

const app = express(); app.use(bodyParser.json());

const userActivityWebhook = twitterWebhooks.userActivity({ serverUrl: 'https://[MY_SERVER_IP]:443/', route: '/', //default : '/' consumerKey: '[MY CONSUMER KEY]', consumerSecret: '[MY CONSUMER SECRET]', accessToken: '[MY APP ACCESS TOKEN]', //this is the same used into userActivityWebhook.subscribe parameters accessTokenSecret: '[MY APP ACCESS TOKEN SECRET]', //this is the same used into userActivityWebhook.subscribe parameters environment: 'myEnv', //default : 'env-beta' app });

//Register your webhook url - just needed once per URL userActivityWebhook.register();

//Subscribe for a particular user activity userActivityWebhook.subscribe({ userId: '[MY USER ID]', accessToken: '[MY ACCESS TOKEN]', //this is like '[MY USER ID]-[OTHER VALUES]' accessTokenSecret: '[MY ACCESS TOKEN SECRET]' }) .then(function (userActivity) { userActivity .on('favorite', (data) => console.log (userActivity.id + ' - favorite')) .on ('tweet_create', (data) => console.log (userActivity.id + ' - tweet_create')) .on ('follow', (data) => console.log (userActivity.id + ' - follow')) .on ('mute', (data) => console.log (userActivity.id + ' - mute')) .on ('revoke', (data) => console.log (userActivity.id + ' - revoke')) .on ('direct_message', (data) => console.log (userActivity.id + ' - direct_message')) .on ('direct_message_indicate_typing', (data) => console.log (userActivity.id + ' - direct_message_indicate_typing')) .on ('direct_message_mark_read', (data) => console.log (userActivity.id + ' - direct_message_mark_read')) .on ('tweet_delete', (data) => console.log (userActivity.id + ' - tweet_delete')) }).catch(function (error) { console.log("Promise Rejected: "+JSON.stringify(error)); });

//listen to any user activity userActivityWebhook.on ('event', (event, userId, data) => console.log (userId + ' - favorite'));

//listen to unknown payload (in case of api new features) userActivityWebhook.on ('unknown-event', (rawData) => console.log (rawData));

const server = https.createServer({ key: fs.readFileSync('./key.pem'), cert: fs.readFileSync('./cert.pem'), passphrase: '[MY_PASSPHRASE]', hostname: '[MY_SERVER_IP]', port: 443, path: '/', method: 'GET' },app);

server.listen(443);

var http = require('http'); http.createServer(app).listen(80);

app.use(function(req, res, next) { if (req.secure) { next(); } else { res.redirect('https://' + req.headers.host + req.url); } });

`

As you can see into the userActivityWebhook.subscribe parameters I have used the same of userActivityWebhook; is this right?

super-ienien commented 5 years ago

you should'nt include the port number in the serverUrl. Twitter doesn't allow this. Maybe that explain why you are getting Error: Webhook URL does not meet the requirements.

nichis commented 5 years ago

Same error also without the port number. Could it be a problem the self signed SSL certificate?

super-ienien commented 5 years ago

in your code you wrote that : serverUrl: 'https://[MY_SERVER_IP]:443/',

SSL certificate are bound to domains so if you are providing your server's IP instead of your server's domain twitter will not connect to your server via it's IP with HTTPS because the certificate is not valid for your server's IP.

You should try : serverUrl: 'https://[MY_SERVER_DOMAIN]',. With [MY_SERVER_DOMAIN] equal to the domain you used to generate your SSL certificate.