Skip to content

Instantly share code, notes, and snippets.

@nisc
Created January 27, 2012 09:07
Show Gist options
  • Save nisc/1687881 to your computer and use it in GitHub Desktop.
Save nisc/1687881 to your computer and use it in GitHub Desktop.
Twitter per-user permissions
/*
* Simple example that shows how to set the requested Twitter access
* permissions on a per-user basis.
*
* For example, this is useful if you only require Direct Message permissions
* from a few users and simple read access for the rest.
*
* 1. Set up your application to request R/W/DM by default in
* the app settings (https://dev.twitter.com/apps/)
* 2. Set the OAUTH_KEY and OAUTH_SECRET environment variables and start this server.
* 3. Go to http://localhost:1337/auth. Use '?scope=read' or '?scope=write' to
* limit the requested access level.
* 4. Follow @niscr (http://nisc.me)!
*/
var KEY = process.env.OAUTH_KEY;
var SECRET = process.env.OAUTH_SECRET;
var REQUEST_URL = 'https://api.twitter.com/oauth/request_token';
var TOKEN_URL = 'https://api.twitter.com/oauth/access_token';
var REDIRECT_URL = 'http://localhost/';
var express = require('express')
, OAuth = require('oauth').OAuth
, app = express.createServer();
app.get('/auth', function(req, res){
var scope = req.query.scope || '';
var oa = new OAuth(
REQUEST_URL+'?x_auth_access_type='+scope, TOKEN_URL,
KEY, SECRET, '1.0', REDIRECT_URL, 'HMAC-SHA1'
);
oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results){
if (error) {
console.log(error);
res.send('fail');
}
else {
console.log('oauth_token: ' + oauth_token);
console.log('oauth_token_secret: ' + oauth_token_secret);
res.redirect('https://twitter.com/oauth/authorize?oauth_token='+oauth_token)
}
});
});
app.listen(1337);
@csanz
Copy link

csanz commented Jan 27, 2012

you should do some nodejs hacking for us

@nisc
Copy link
Author

nisc commented Jan 27, 2012

I only dig the useless features, look at this stuff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment