npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fbgraphapi

v0.5.1

Published

Simple facebook api client

Downloads

145

Readme

Facebook nodejs

A simple module for querying Facebook graph api and fql

Usage example

// run first: npm install express fbgraphapi body-parser cookie-parser express-session
const express = require('express');
const fbgraph = require('fbgraphapi');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const session = require('express-session');

const app = express();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser())
app.use(session({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: true,
  cookie: { secure: false }
}))

app.use(fbgraph.auth( {
		appId : "...",
		appSecret : "...",
		redirectUri : "http://0.0.0.0:3000/",
		apiVersion: "v2.9",
		skipUrlPatterns: ["/favicon.ico"]
	}));

app.get('/login', function(req, res) {
	console.log('Start login');
	fbgraph.redirectLoginForm(req, res);
});

app.get('/', function(req, res) {
	if (!req.hasOwnProperty('facebook')) {
		console.log('You are not logged in');
		return res.redirect('/login');
	}
	/* See http://developers.facebook.com/docs/reference/api/ for more */
	req.facebook.graph('/me', function(err, me) {
	    console.log(me);
	});

	req.facebook.graph('/me?fields=id,name', function(err, me) {
	    console.log(me);
	});

	req.facebook.me(function(err, me) {
	    console.log(me);
	});

	// /me/likes
	req.facebook.my.likes(function(err, likes) {
	    console.log(likes);
	});

	res.end("Check console output");
});

app.listen(3000);

Or if have a valid access token for instance from javascript fb connect

var fb = new fbgraph.Facebook(accessToken, 'v2.2');
fb.me(function(err, me) {
	console.log(me);
});

Or do stuff on behalf of the app or user with granted permissions

var fb = new fbgraph.Facebook(fbgraph.getAppId() + '|' + fbgraph.getAppSecret());
fb.post('/{user-id}/feed', {message: 'Hello world'},function(err, res) {
	console.log(err, res);
});

Facebook API reference

Visit the links bellow for API documentation of Facebook API https://developers.facebook.com/docs/graph-api/using-graph-api

Methods

auth(config)

config is an object with those properties

  • appId Facebook application Id
  • appSecret Secret hash key generated by Facebook
  • redirectUri The url to redirect to when user logged in.
  • apiVersion Which api version to use, example v2.2
  • scope Permissions/scope that your application asks for, optional and default empty.
  • skipUrlPatterns Array of patterns which to not apply authentication on. They can be regexp or string. If string a regexp will be created with wildcard appending at the end. If you want an exact url make sure specify regexp.

authenticate(req, res, next)

This method is returned when calling auth() above. When loggin is successfull it will assign a Facebook instance to req (see example above).

redirectLoginForm(req, res)

This method will redirect user to Facebook login form.

destroySession(req, res, clearCookie)

This method is for logging out user or for any reason want to clear user's logged-in info

getAppId()

return app id, set via auth()

getAppSecret()

return app secret, set via auth()

Classes

Facebook(accessToken, apiVersion)

  • accessToken Valid access token from Facebook (oauth_token).
  • apiVersion Which api version to use
  • .graph(path, callback) Main method for making call to Facebook API. Path can contain only /me/likes or with params like /me/likes?limit=3
  • .post(path, params, callback) or post(path, callback) Publishing to Facebook, see https://developers.facebook.com/docs/graph-api/using-graph-api/v2.0#publishing for detail. Path can contain post data such as /123/feed?message=hello
  • .update(path, params, callback) or update(path, callback) Updating a post on Facebook, see https://developers.facebook.com/docs/graph-api/using-graph-api/v2.0#updating for defail. Path can contain post data such as /123_123456789?message=edited
  • .delete(path, callback) Deleting a post on Facebook, see https://developers.facebook.com/docs/graph-api/using-graph-api/v2.0#deleting for defail
  • .fql(query, callback) Specific method for making fql query to Facebook API
  • .search(params, callback) params is an object which properties are any param based on this https://developers.facebook.com/docs/graph-api/using-graph-api#search
  • .me(callback, fields) Specific method to get info of the logged user, same as /me when using graph-method. Fields is comma-separated for instance fields=id,name
  • .my Instance of My class

Usage: if have a valid accessToken for instance from js login

var fb = new Facebook(accessToken, 'v2.2');
fb.me(function(err, me) {
	console.log(me);
});

My(facebook)

This class uses internally to create object my (property in Facebook). This object wrap the me-object. Each connection type is a method. This means that you can make a call like

req.facebook.my.friends(function(err, friends) {
	console.log(friends)
});

// OR for checkins
req.facebook.my.checkins(function(err, checkins) {
	console.log(checkins)
});

Supported connection types are:

  • friends
  • feed
  • likes
  • movies
  • music
  • books
  • albums
  • notes
  • permissions
  • photos
  • videos
  • events
  • groups
  • checkins
  • locations

If you can not find a connection that Facebook has but not here you can use connection-method

req.facebook.my.connection('{connection type}', function(err, result) {
	console.log(result)
});