douchu5131 2014-08-04 18:21
浏览 14

如何让martini-oauth2与Facebook合作?

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条回答 默认 最新

  • dongmouhao7438 2014-08-10 02:25
    关注

    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"
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题