Created
April 19, 2011 03:27
-
-
Save tiqtech/926756 to your computer and use it in GitHub Desktop.
Port of server-side Facebook authentication example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var url = require("url"); | |
| var express = require('express'); | |
| var https = require("https"); | |
| var server = express.createServer(); | |
| function getContent(options, callback) { | |
| var req = https.request(options, function(res) { | |
| var body = ""; | |
| res.on('data', function (chunk) { | |
| body += chunk; | |
| }); | |
| res.on('end', function() { | |
| callback(body); | |
| }); | |
| }); | |
| req.end(); | |
| } | |
| server.get("/fbauth", function(req, res) { | |
| var appId = "YOUR_APP_ID"; | |
| var appSecret = "YOUR_SECRET"; | |
| var myUrl = "http://YOUR_SERVER/fbauth"; | |
| var code = url.parse(req.url, true).query.code; | |
| if(!code) { | |
| var dialogUrl = "http://www.facebook.com/dialog/oauth?client_id=" + appId + "&redirect_uri=" + encodeURIComponent(myUrl); | |
| res.redirect(dialogUrl); | |
| return; | |
| } | |
| var tokenUrl = { | |
| host: "graph.facebook.com", | |
| method: "GET", | |
| path: "/oauth/access_token?client_id=" + appId + "&redirect_uri=" + encodeURIComponent(myUrl) + "&client_secret=" + appSecret + "&code=" + code | |
| } | |
| getContent(tokenUrl, function(token) { | |
| var graphUrl = { | |
| host: "graph.facebook.com", | |
| method: "GET", | |
| path: "/me?" + token | |
| }; | |
| getContent(graphUrl, function(user) { | |
| user = (user) ? JSON.parse(user) : {}; | |
| res.send("Hello <a href='" + user.link + "'>" + user.name + "</a>"); | |
| }) | |
| }); | |
| }); | |
| server.listen(80); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment