如何使用 client4 go 驱动程序通过个人访问令牌进行官方认证?
来源:stackoverflow
时间:2024-03-02 18:24:26 428浏览 收藏
Golang小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《如何使用 client4 go 驱动程序通过个人访问令牌进行官方认证?》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!
我们正在运行两个最重要的服务器。
我们有一个 python 进程,使用社区 Python 驱动程序的个人访问令牌通过 https://github.com/Vaelor/python-mattermost-driver 登录。 此过程有一个不会超时的会话,这是使用个人访问令牌登录的好处之一。 https://docs.mattermost.com/developer/personal-access-tokens.html。
我们使用 client4 go 驱动程序的用户名和密码登录,这可以工作,但一段时间后就会超时。看来无法使用个人访问令牌通过官方客户端 4 驱动程序登录。
Mattermost Client4 代码的文档位于 https://godoc.org/github.com/mattermost/platform/model#Client
client4 的来源位于 https://github.com/mattermost/mattermost-server/blob/master/model/client4.go
看起来最接近的方法是使用用户名和密码登录,然后通过 client.MockSession 设置身份验证令牌,但测试失败。
使用 client4 go 驱动程序使用个人访问令牌登录的官方方式是什么?
解决方案
关键是不登录、设置个人访问令牌并调用client.getme。 在查看了 python 驱动程序源代码后,这一点变得很清楚。 相关片段是。
fmt.println("loginasthebotuser", "using personal access token") client.authtoken = mattermost_personal_access_token client.authtype = model.header_token if user, resp := client.getme(""); resp.error != nil { println("there was a problem logging into the mattermost server. are you sure the access token is valid?") printerror(resp.error) } else { fmt.println(client) botuser = user result = true }
上下文。
/* mattermost_user_email, mattermost_user_password and mattermost_personal_access_token are globals set from reading a .env file on startup */ func LoginAsTheBotUser() bool { fmt.Println("LoginAsTheBotUser") fmt.Println("LoginAsTheBotUser", time.Now()) var result bool = false if mattermost_personal_access_token == "" { fmt.Println("LoginAsTheBotUser", "Getting access token from login with username and password") if user, resp := client.Login(mattermost_user_email, mattermost_user_password); resp.Error != nil { println("There was a problem logging into the Mattermost server. Are you sure ran the setup steps from the README.md?") PrintError(resp.Error) } else { fmt.Println(client) botUser = user result = true } } else { fmt.Println("LoginAsTheBotUser", "Using personal access token") client.AuthToken = mattermost_personal_access_token client.AuthType = model.HEADER_TOKEN if user, resp := client.GetMe(""); resp.Error != nil { //if user, resp := client.Login(mattermost_personal_access_token); resp.Error != nil { println("There was a problem logging into the Mattermost server. Are you sure the access token is valid?") PrintError(resp.Error) } else { fmt.Println(client) botUser = user result = true } } fmt.Println("LoginAsTheBotUser", time.Now()) fmt.Println("LoginAsTheBotUser") return result }
今天关于《如何使用 client4 go 驱动程序通过个人访问令牌进行官方认证?》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习