- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试使用 vk auth与 martini .但是编译时出错:
/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
。
@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 设置,你必须像这个截图一样添加你的域
之后您将被重定向到 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/
我在尝试使用 Vulkan Hpp 唯一句柄来存储缓冲区及其分配的内存时遇到了以下难题。我声明句柄 vk::UniqueBuffer vertex_buffer; vk::UniqueDeviceMe
当我在 vk.com 墙上分享网站链接时,它会显示网站的标题和缩略图。如何使用 vk ios sdk 做到这一点?我试图在消息中分享一个链接,例如 VKRequest *request = [[VKA
我正在使用以下 php 代码: $vk = new VK($app_id, $api_secret); $user_wall = $vk->api('newsfeed.get', array(
我正在尝试与包括 VK 在内的其他应用程序共享我的 Android 应用程序中的音频文件:https://play.google.com/store/apps/details?id=com.vkont
我正在尝试按照 here 和 here 的描述授权 vk.com 用户 (OAuth 2.0)。问题:每次我只看到一个空白屏幕,除了对 GC 的一次调用之外似乎什么也没有发生。我测试了我的 WebVi
我正在测试 vk.com api 以便能够执行搜索。我可以获得 token ,但是当我使用它时,它总是返回错误:“access_token 已过期”能不能帮我看看是不是代码错了,还是vk的配置? 非常
美好的一天。关于使用库VK的问题。如何获取头像,使用VKSDK\在 Internet 上找不到合适的信息。 最佳答案 在您的请求中,您需要指定以下字段之一:photo_50 或 photo_100 或
我正在尝试在 Swift 2.0 项目中设置 VK iOS SDK。我收到一个错误,我不知道为什么会发生。 AppDelegate.swift: // // AppDelegate.swift //
初始化这个结构的类似 C 的方法是: VkDeviceQueueCreateInfo queueCreateInfo = {}; queueCreateInfo.sType = VK_STRU
如何嵌入来自 vk.com 的视频并指定视频开始的时间(例如:视频应该从中间开始,或从第 5 秒开始),以及隐藏控件,就像在 YouTube 上一样? 一个简单的嵌入看起来像: 为了让它自动播放,我
我正在尝试授权我的独立应用程序。但在我单击“允许”后,它总是重定向到 http://oauth.vk.com/error?err=2 并将其作为响应正文: {"error":"invalid_requ
我面临这样的问题...我确实通过createWallPost在墙上发布,简单文本,从几个字符到非常大的文本的任意长度的纯文本...通过一次发布。下一次发布一次,同样的记录会报错。 ... 我很困惑
我想通过 vk.com 进行授权。我下载this库,但是没有正常的例子怎么用呢?我尝试下一个代码,但它对我不起作用。 package ua.khuta.testVkSdk; import an
我最近在 vk.vom 上看到了一些有趣的 css 技巧 background: url(/images/icons/mono_iconset.gif?8) no-repeat left -237px
我需要写信给List大学名单。我已经写好了要列出的国家和城市。但在大学中,数据的表现略有不同。有 {"response":[201,我不知道如何处理它。我成功地从 here 得到了国家.来自here我
我正在尝试获取电子邮件地址。成功登录后,我有名字,姓氏,除了电子邮件。请帮忙。 VK.Auth.login(function (response) { if (response
我正在尝试使用 vk auth与 martini .但是编译时出错: /goPath/vkAuthTry2.go:38: undefined: YourRedirectFunc 问题是如何定义Your
使用 Perl,我有两个相似的语法, if ($a && $b) { exit() } do { exit() } if ($a && $b) 我相信这些应该是同一件事,但是最上面的一个创建了 nul
vk.com 中使用 zend_oauth 进行身份验证时出现问题 消息:HTTP 请求中出现错误:无法在 TCP 连接 oauth.vk.com 上启用加密:确保为环境正确设置了“sslcafile
我正在使用社交网络 Vkontakte 作为 Oauth2 授权服务器。所以我有几个步骤:1) 使用 request_type=code 请求获取代码2)当我发送访问 token uri的请求时获取a
我是一名优秀的程序员,十分优秀!