Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post a tweet with Meteor.js, Twitter and Oauth

i have a little problem with Meteor and Twitter. All i want to do is posting a tweet through a click on a button. For this I have to authenticate myself over Oauth to the Twitterservice.

At the moment i am doing the authentification in a really complicated way springing from client to server and back. But now I found the function Meteor.loginWithTwitter. Originally I thought this function is only for logging you into your own application with the Twitterkeys, now i am not so sure anymore. Probably I can also use it for my problem. Because it seems that the Oauth-Process is completely (and in a simple way) implemented in Meteor.

Sadly i cann't find any documentation or examples for just logging in and getting the final oauth_token. And so all i got from Meteor back then i try the following code, is this errormessage:

Erromessage: Accounts.ConfigError {message: "Service not configured"}

Meteor.loginWithTwitter( function(err){
  if (err){
    console.log(err)
  }else{
    console.log("yeah");
 }
});

I know i have to enter somewhere my Appinformation like the Consumer key, but i have no idea where. Can someone help me out and knows some examples for me? Or knows if i am even on the right track?

Thanks and greetings Philipp

like image 307
kruemelnerd Avatar asked Nov 07 '12 15:11

kruemelnerd


People also ask

How do I call the Twitter API from a Node JS script?

PS: the #!/usr/bin/env node part is useful if you want to make your Node.js script an executable ( chmod +x oauth1.js) With OAuth 2.0 the authentication is even simpler, but more limited on its actions, as you can read above. You’ll need both the TWITTER_CONSUMER_KEY and TWITTER_CONSUMER_SECRET to get a Bearer token to call the Twitter API.

How do I set up Twitter to post tweets on my behalf?

Click " Edit " and switch to " Read and Write " in order to allow our application to post tweets on your behalf: Click on " Keys and tokens " on the right side of " Settings " and regenerate " Api Key and Secret " since we didn't copy them from any of the previous screens: Confirm the action and you will see a modal with the keys.

How do I make API requests on behalf of a Twitter account?

Many endpoints of the Twitter API use the OAuth 1.0a method to act, or make API requests, on behalf of a Twitter account. With a Twitter developer app, you can make requests on behalf of a Twitter account as long as that user authenticated through the Twitter login screen to your app.

How do I authenticate with OAuth on the Twitter platform?

There are different ways to authenticate with OAuth on the Twitter platform. Below you can read a bit more about OAuth 1.0a and OAuth 2.0 Bearer authentication methods and a step-by-step explanation of the usage in Node.js Many endpoints of the Twitter API use the OAuth 1.0a method to act, or make API requests, on behalf of a Twitter account.


3 Answers

The easiest way of doing this: Add the accounts-ui package:

meteor add accounts-ui accounts-twitter

and in your template do

{{loginButtons}}

On the first start of the application, a click on the login button will guide you through the setup process. You will create a Twitter application and copy the consumer key and consumer secret into a form, that meteor presents you. Afterwards you can log in using Twitter.

Make sure to use the latest Meteor version (0.5.2 at this moment)

like image 123
geekonaut Avatar answered Nov 05 '22 14:11

geekonaut


You can also config your consumer key and secret with code, this is an example with weibo but its work for twitter, google etc... (server side) :

// first, remove configuration entry in case service is already configured
Accounts.loginServiceConfiguration.remove({
    service: "weibo"
});
Accounts.loginServiceConfiguration.insert({
    service: "weibo",
    clientId: "1292962797",
    secret: "75a730b58f5691de5522789070c319bc"
});
like image 22
Baltox Avatar answered Nov 05 '22 13:11

Baltox


You need to add what @Tom31 suggested in your server side, i.e., I have a /server/server.js

Accounts.loginServiceConfiguration.remove({"service": "twitter"});
Accounts.loginServiceConfiguration.insert({
 "service": "twitter",
 "consumerKey" : "<yours>",
 "secret" : "<yours>"
});

Finally, your access token are stored in your user at the database but this information it is not propagated to the client and, if you want to have access to it, you new to create a server side method and access it through Meteor.call or Meteor.apply

Updated: Example of my server side method

I've created my server side method like this:

Meteor.methods({
...
  userGet: function(id) {
   return removeSensibleFields( Meteor.users.findOne({ _id: id }) );
  }
...
});

where removeSensibleFields is a method to remove all unnecessary/private information that may not be sent back to the client

like image 21
bitIO Avatar answered Nov 05 '22 13:11

bitIO