How to get martini-oauth2 to work with Facebook?

337 Views Asked by At

I tried the following code example against facebook but it keeps redirecting me to oauth2error. I am following the directions exactly as per the example but it just doesn't seem to work. I am pretty new to golag but despite my best efforts I can't seem to make things work.

package main

import (
    "log"
    "net/http"

    "github.com/go-martini/martini"
    gooauth2 "github.com/golang/oauth2"
    "github.com/martini-contrib/oauth2"
    "github.com/martini-contrib/sessions"
)    

func main() {
    m := martini.Classic()
    m.Use(sessions.Sessions("my_session", sessions.NewCookieStore([]byte("secret123"))))
    m.Use(oauth2.Facebook(&gooauth2.Options{
    ClientID:     "XXXX",
    ClientSecret: "XXXX",
    RedirectURL:  "http://localhost.foobar.com:8080/",
    Scopes:       []string{"public_profile"},
    }))
m.Get("/", func(tokens oauth2.Tokens) string {
    if tokens.IsExpired() {
        return "not logged in, or the access token is expired"
    }
    return "logged in"
})
m.Get("/restrict", oauth2.LoginRequired, func(tokens oauth2.Tokens) string {
    return tokens.Access()
})
m.Get("/success", oauth2.LoginRequired, func(tokens oauth2.Tokens) string {
    return tokens.Access()
})

log.Fatal(http.ListenAndServe(":8080", m))
}

Can anyone point out what am i doing wrong here?

1

There are 1 best solutions below

0
On

It looks like your callback url is not set correctly. See my similar config for github:

m.Use(oauth2.Github(&golang_oauth2.Options{
    ClientID:     "0.0",
    ClientSecret: "o.o",
    RedirectURL:  "http://localhost:3000/oauth2callback",
    Scopes:       []string{"user:email", "read:org"},
}))

Try setting your redirect url to:

RedirectURL:  "http://localhost.foobar.com:8080/oauth2callback"