Login with Twitter

What You'll Need

  1. Backbench account - Sign Up, the personal account will always remain free to use.

  2. Twitter Developer account- Apply for a Developer account.

Prerequisites for the app

  1. Sign In to backbench, and create or use a bench.

  2. Start by creating a new app on twitter, enter a valid callback url in below format, https://USERID-BENCHID.backbench.io/cb ( found in Endpoints section of Backbench. )

  3. Select Enable Sign In with Twitter at Allow this application to be used to Sign in with Twitter and hit save.

  4. Keep a note of following parameter's from twitter app's dashboard.

    consumer_key, consumer_secret, callback_url

Demo

Node.js Sample

https://mybench-bbsamples.backbench.io/twitter.html

Frontend

  1. Create a html for Twitter's login in File manager section. (note: Do not forget to enable File manager from endpoint's section)

Backend

Create a module login.js for Node, login for BBLANG. Replace consumer_key, consumer_secret, and callback_url.

For Node, map login.js module to /login and for BBLANG, map twitter:login to /login in Endpoints section.

Now we'll create a function which responds after twitter redirects to /cb. Make cb.js for node and cb for bblang (twitter:callback) and map it to /cb.

For Node.js App:

  1. Select +, in the upper right corner to create a Module. For example, say "package.json" and select CREATE or hit Enter.

  2. Copy and paste the code module from below.

A package.json file lists the packages that your project depends on. allows you to specify the versions of a package that your project can use using semantic versioning rules.

Logging in

Obtaining a request token by sending a POST request to /oauth/request_token with following as headers. (if you are using nodejs request library then you can send these params in oauth param of request)

Redirect to Twitter

You'll get oauth_token in response of above. Next step is to redirect to Twitter's authentication.

https://api.twitter.com/oauth/authenticate?oauth_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Exchange request_token with access_token

Convert your request_token to token, by sending a POST request to /oauth/access_token, with following headers:

where oauth_token and oauth_verifier you'll get as query parameter. (after twitter's redirection)

For a comprehensive guide, refer official doc https://dev.twitter.com/web/sign-in/implementing.

User Permission:

Can be changed on Twitter's app setting page.

Confirming Identity:

Once you've obtained an Token, you can start making authenticated API requests on behalf of the user by sending request.

https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=peeyu5h&count=2

with header containing consumer_key, consumer_secret, token and token_secret.

Last updated