Skip to content

Instantly share code, notes, and snippets.

@deadprogram
Created January 9, 2012 03:14
Show Gist options
  • Save deadprogram/1580826 to your computer and use it in GitHub Desktop.
Save deadprogram/1580826 to your computer and use it in GitHub Desktop.
Node.js Sample Application for AT&T Alpha API Foundry
var express = require('express');
var OAuth = require('oauth').OAuth2;
var app = express.createServer();
var _foundryConsumerKey = "YOURFOUNDRYKEY";
var _foundryConsumerSecret = "YOURFOUNDRYSECRET";
var _redirectUri = "http://hello-att-foundry-node.heroku.com/auth/callback";
var consumer = new OAuth(_foundryConsumerKey,
_foundryConsumerSecret,
"https://auth.tfoundry.com/",
"oauth/authorize",
"oauth/token");
app.get('/', function(req, res){
res.send('<p><a href="/auth">Try to authorize</a>.</p>');
});
app.get('/auth', function(req, res){
var redirectUrl = consumer.getAuthorizeUrl({redirect_uri: _redirectUri, response_type: "client_credentials"})
res.redirect(redirectUrl);
});
app.get('/auth/callback', function(req, res){
consumer.getOAuthAccessToken(req.query["code"], {redirect_uri: _redirectUri}, function( error, access_token, refresh_token ){
if( error ) {
res.send("Error on getOAuthAccessToken : " + sys.inspect(error), 500);
}
else {
consumer.get("https://hello-att-foundry.heroku.com/hello.json", access_token, function (error, data, response) {
if( error ) {
res.send("Error getting greeting : " + sys.inspect(error), 500);
} else {
res.send('Your greeting is: ' + JSON.parse(data).greeting)
}
})
}
});
});
app.listen(parseInt(process.env.PORT || 80));
{
"name": "hello",
"version": "0.0.1",
"dependencies": {
"express": "*",
"oauth": "*"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment