facebook

package module
v0.0.0-...-e3366e6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2015 License: MIT Imports: 16 Imported by: 15

README

Facebook API wrapper in Go

GoDoc

This is a work in progress. It is currently used by deiwin/luncher-api and only implements the functionality needed by that project.

Tutorial

Configuration

Using $FACEBOOK_APP_SECRET and $FACEBOOK_APP_ID environment variables:

redirectURL := "http://your.domain.com/login/facebook/redirected"
scopes := []string{"manage_pages", "publish_actions", "whatever"}
facebookConfig := facebook.NewConfig(redirectURL, scopes)

However, if you wish, you can also manage your app secret and id differently:

facebookConfig := facebook.Config{
  AppID:       "your_app_id",
  AppSecret:   "your_app_secret",
  RedirectURL: "http://your.domain.com/login/facebook/redirected",
  Scopes:      []string{"manage_pages", "publish_actions", "whatever"},
}
Authentication
Redirecting users to Facebook for login
facebookAuthenticator := facebook.NewAuthenticator(facebookConfig)

// In an handler for the login request
session := "your_identifier_for_the_current_user_session"
redirectURL := facebookAuthenticator.AuthURL(session)
http.Redirect(w, r, redirectURL, http.StatusSeeOther)
Handling users redirected back from Facebook
// In an handler for the RedirectURL in the configuration.
// E.g "http://your.domain.com/login/facebook/redirected"
session := "your_identifier_for_the_current_user_session"
tok, err := facebookAuthenticator.Token(session, r)
if err != nil {
  if err == facebook.ErrMissingState {
    http.Error(w, "Expecting a 'state' value", http.StatusBadRequest)
  } else if err == facebook.ErrInvalidState {
    http.Error(w, "Invalid 'state' value", http.StatusForbidden)
  } else if err == facebook.ErrMissingCode {
    http.Error(w, "Expecting a 'code' value", http.StatusBadRequest)
  }
  http.Error(w, "", http.StatusInternalServerError)
}
Using the API
Receiving information about the current user
api := fb.auth.APIConnection(tok)
user, err := api.Me()
if err != nil {
  ...
}
return user.ID

See the GoDoc for the API for more options.

Documentation

Overview

Package facebook wraps the Facebook API in Go.

See the README on Github for more info: https://github.com/deiwin/facebook

Index

Constants

View Source
const ISO8601 = "2006-01-02T15:04:05-0700"

Variables

View Source
var (
	ErrMissingState = errors.New("A Facebook redirect request is missing the 'state' value")
	ErrInvalidState = errors.New("A Facebook redirect request's 'state' value does not match the session")
	ErrMissingCode  = errors.New("A Facebook redirect request is missing the 'code' value")
	ErrNoSuchPage   = errors.New("The user does not have access to that page")
)

Functions

This section is empty.

Types

type API

type API interface {
	// GET /me
	//
	// https://developers.facebook.com/docs/graph-api/reference/v2.4/user#read
	Me() (*model.User, error)

	// GET /me/accounts
	//
	// https://developers.facebook.com/docs/graph-api/reference/v2.4/user/accounts#read
	Accounts() (*model.Accounts, error)

	// GET /{page-id}
	//
	// https://developers.facebook.com/docs/graph-api/reference/page/
	Page(pageID string) (*model.Page, error)

	// POST /{page-id}/feed
	//
	// https://developers.facebook.com/docs/graph-api/reference/v2.4/page/feed#publish
	PagePublish(pageAccessToken, pageID string, post *model.Post) (*model.PostResponse, error)

	// POST /{page_id}/photos
	//
	// https://developers.facebook.com/docs/graph-api/reference/page/photos/#Creating
	PagePhotoCreate(pageAccessToken, pageID string, photo *model.Photo) (*model.PhotoResponse, error)

	// GET /{post-id}
	//
	// https://developers.facebook.com/docs/graph-api/reference/v2.4/post#read
	Post(pageAccessToken, postID string) (*model.PostResponse, error)

	// POST /{post-id}
	//
	// https://developers.facebook.com/docs/graph-api/reference/v2.4/post#updating
	PostUpdate(pageAccessToken, postID string, post *model.Post) error

	// DELETE /{post-id}
	//
	// https://developers.facebook.com/docs/graph-api/reference/v2.4/post#deleting
	PostDelete(pageAccessToken, postID string) error
}

API provides access to the Facebook API graph methods

func NewAPI

func NewAPI(client *http.Client) API

NewAPI creates an instance of the API using the provided *http.Client. It expects all the authentication to be handled by the http client.

type Authenticator

type Authenticator interface {
	// AuthURL returns a Facebook URL the user should be redirect to. The user
	// will then be asked to log in by Facebook at that URL and will be redirected
	// back to the configured RedirectURL.
	AuthURL(state string) string
	// Token get's the longer term user access token from the redirect request.
	// Also checks that the provided state matches that of the redirect request and
	// returns "", ErrInvalidState if it doesn't.
	Token(state string, r *http.Request) (*oauth2.Token, error)
	// APIConnection returns an API instance that can be used to make authenticated
	// requests to the Facebook API.
	APIConnection(tok *oauth2.Token) API
	// PageAccessToken retrieves a page access token of the specified page if the
	// user has access to that page and returns "", ErrNoSuchPage if they don't.
	PageAccessToken(tok *oauth2.Token, pageID string) (string, error)
}

Authenticator provides the authentication functionality for Facebook users using Facebook's OAuth

func NewAuthenticator

func NewAuthenticator(conf Config) Authenticator

NewAuthenticator initializes and returns an Authenticator

type Config

type Config struct {
	AppID       string
	AppSecret   string
	RedirectURL string
	Scopes      []string
}

func NewConfig

func NewConfig(redirectURL string, scopes []string) Config

type Error

type Error struct {
	Message string `json:"message"`
	Type    string `json:"type"`
	Code    int    `json:"code"`
}

func (*Error) Error

func (e *Error) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL