gpt4 book ai didi

go - 社交网络 vk auth with martini

转载 作者:IT王子 更新时间:2023-10-29 01:39:38 24 4
gpt4 key购买 nike

我正在尝试使用 vk authmartini .但是编译时出错:

/goPath/vkAuthTry2.go:38: undefined: YourRedirectFunc

问题是如何定义YourRedirectFunc 函数。或者,如果更广泛地询问,我需要使用 vk 社交网络身份验证的 martini 应用程序的工作示例,或者更广泛地使用 vk 的任何 golang 网站的示例> 身份验证。

完整代码:

package main

import (
"github.com/go-martini/martini"
"github.com/yanple/vk_api"
"net/http"
)

var api vk_api.Api

func prepareMartini() *martini.ClassicMartini {
m := martini.Classic()
m.Get("/somePage", func(w http.ResponseWriter, r *http.Request) {
// And receive token on the special method (redirect uri)
currentUrl := r.URL.RequestURI() // for example "yoursite.com/get_access_token#access_token=3304fdb7c3b69ace6b055c6cba34e5e2f0229f7ac2ee4ef46dc9f0b241143bac993e6ced9a3fbc111111&expires_in=0&user_id=1"
accessToken, userId, expiresIn, err := vk_api.ParseResponseUrl(currentUrl)
if err != nil {
panic(err)
}
api.AccessToken = accessToken
api.UserId = userId
api.ExpiresIn = expiresIn
w.Write([]byte("somePage"))
})
return m
}

func main() {
authUrl, err := api.GetAuthUrl(
"domain.com/method_get_access_token", // redirect URI
"token", // response type
"4672050", // client id
"wall,offline", // permissions https://vk.com/dev/permissions
)
if err != nil {
panic(err)
}
YourRedirectFunc(authUrl)
prepareMartini().Run()
}

更新

我根据@Elwinar 的回答编辑了我的代码:

package main

import (
"fmt"
"github.com/go-martini/martini"
"github.com/yanple/vk_api"
"net/http"
)

var api vk_api.Api

func prepareMartini() *martini.ClassicMartini {
m := martini.Classic()
// This handler redirect the request to the vkontact system, which
// will perform the authentification then redirect the request to
// the URL we gave as the first paraemeter of the GetAuthUrl method
// (treated by the second handler)
m.Get("/vk/auth", func(w http.ResponseWriter, r *http.Request) {
var api vk_api.Api
authUrl, err := api.GetAuthUrl("http://localhost:3000/vk/token", "token", "4672050", "wall,offline")
if err != nil {
panic(err)
}

http.Redirect(w, r, authUrl, http.StatusFound)
})

// This handler is the one that get the actual authentification
// information from the vkontact api. You get the access token,
// userid and expiration date of the authentification session.
// You can do whatever you want with them, generally storing them
// in session to be able to get the actual informations later using
// the access token.
m.Get("/vk/token", func(w http.ResponseWriter, r *http.Request) {
accessToken, userId, expiresIn, err := vk_api.ParseResponseUrl(r.URL.String())
if err != nil {
panic(err)
}
fmt.Println(accessToken)
fmt.Println(userId)
fmt.Println(expiresIn)
})
return m
}

func main() {
prepareMartini().Run()
}

现在没有编译错误,但仍然无法登录。当我打开 http://localhost:3000/vk/auth 时,我被重定向到页面...

https://oauth.vk.com/authorize?client_id=MY_APP_ID&redirect_uri=localhost%3A3000%2Fvk%2Ftoken&response_type=token&scope=wall%2Coffline

... 并得到以下浏览器输出:

{"error":"invalid_request","error_description":"redirect_uri is incorrect, check application domain in the settings page"}

当然,我粘贴了我的应用程序 ID,而不是 4672050。这个应用程序是专门为 localhost:3000 生成的。也许我需要将我的私钥粘贴到某处以进行 oauth,例如 pYFR2Xojlkad87880dLa

更新2

@qwertmax 的回答几乎有效。我已通过 vk 成功登录,但我的代码打印空行而不是 userId 和另一个用户信息:

 accessToken, userId, expiresIn, err := vk_api.ParseResponseUrl(r.URL.String())

fmt.Println(accessToken)
fmt.Println(userId)
fmt.Println(expiresIn)

最佳答案

package main

import (
"fmt"
"github.com/go-martini/martini"
"github.com/yanple/vk_api"
"net/http"
)

var api vk_api.Api

func prepareMartini() *martini.ClassicMartini {
m := martini.Classic()

m.Get("/vk/auth", func(w http.ResponseWriter, r *http.Request) {
var api vk_api.Api
authUrl, err := api.GetAuthUrl("http://localhost:3000/vk/token", "token", "2756549", "wall,offline")

fmt.Println(authUrl)
if err != nil {
panic(err)
}

http.Redirect(w, r, authUrl, http.StatusFound)
})

m.Get("/vk/token", func(w http.ResponseWriter, r *http.Request) {
accessToken, userId, expiresIn, err := vk_api.ParseResponseUrl(r.URL.String())
if err != nil {
panic(err)
}
fmt.Println(accessToken)
fmt.Println(userId)
fmt.Println(expiresIn)
})
return m
}

func main() {
prepareMartini().Run()
}

对于你的 VK 设置,你必须像这个截图一样添加你的域

enter image description here

之后您将被重定向到 http://localhost:3000/vk/token#access_token=some_token&expires_in=0&user_id=0000000

但我不确定您将如何通过“ParseResponseUrl”解析 url,因为 vk 会为您提供“片段 url”。

片段 url 未通过 HTTP 发送以提供服务 - 我认为这可能是个问题。

关于go - 社交网络 vk auth with martini,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29359907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com