我正在try 用golang.org/x/oauth2
包编写一个简单的程序.但我似乎无法用代码交换访问令牌.下面的错误有点误导,因为它说已经使用了授权代码,但我每次调用登录对话框时都会看到一个新代码.我是刚来golang的新手,可能犯了一个基本的错误,任何提示都会非常有用:)
clientOptions, err = oauth2.New(
oauth2.Client("xxxxxx", "22222222222222"),
oauth2.RedirectURL("http://localhost:3000/auth/cb/fb2"),
oauth2.Scope("public_profile", "email", "user_friends"),
oauth2.Endpoint(
"https://www.facebook.com/dialog/oauth",
"https://graph.facebook.com/oauth/access_token",
),
)
func handleFBSetupOauth(w http.ResponseWriter, r *http.Request) {
url := clientOptions.AuthCodeURL("state", "online", "auto")
fmt.Printf("Visit the URL for the auth dialog: %v", url)
http.Redirect(w, r, url, http.StatusFound)
}
func handleFBOauthCB(w http.ResponseWriter, r *http.Request) (int, string) {
var err error
code := r.FormValue("code")
if code == "" {
return 500, "No code!"
}
fmt.Printf("code - %s", code)
t, err := clientOptions.NewTransportFromCode(code)
if err != nil {
log.Fatal(err)
}
client := http.Client{Transport: t}
url := "https://graph.facebook.com/oauth/access_token?client_id=xxxxxxx&redirect_uri=http://localhost:3000/auth/cb/fb2&client_secret=22222222&code=" + code + ""
resp, err := client.Get(url)
我从上一个GET请求中收到以下错误-
{"错误":{"消息":"此授权码已被使用.","类型":"OAutheException","代码":100}
我跟着这两位导游走- Facebook登录流程- https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/v2.2个
最新goauth文件-