Search code examples
node.jsmeteortwittertwitter-oauth

Tweet using application in Twitter with Meteor


I am successful in authorizing a request with application-only authentication. But when I tried to post a tweet with REST API. The error is related to not having an access to post a tweet. So I do a research and found out that Twitter doesn't allow app-only authentication to post tweets.

"And it will not be able to: Post Tweets or other resources;"

But I found the "Your access token" area. Can I use it to post tweet? How can I use it?

Also can I use the same method for commenting?

Here is my code so far,

HTTP.call('POST', 'https://api.twitter.com/oauth2/token', {
            params: { 'grant_type':'client_credentials'},
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
                'Authorization': 'Basic '+base64token
            }
        },function (error, response) {
            if (!error && response.statusCode == 200) {
                token = response.data.access_token;
                HTTP.call('POST', 'https://api.twitter.com/1.1/statuses/update.json', {
                    params: { 'status':data.tweet},
                    headers: {
                        'Authorization': 'Bearer '+token
                    }
                },function (error, response) {
                    if (!error && response.statusCode == 200) {
                        console.log(response);
                    } else {
                        console.error(error.message);
                    }
                }); 
            } else {
                console.error(error.message);
            }
        }); 

Solution

  • It's hard to tell you anything specific if you're not even saying whether you're running that code on the client or on the server side.

    But if you're calling the Twitter API on the server side (as you should to avoid leaking credentials) then you should use one of the many specialized modules to interact with the Twitter API that are available on npm. See this answer for examples:

    It is much easier to use the right tool for the job than reimplementing it yourself, especially if you have trouble doing it yourself which is apparently the case here.