小编典典

大部分情况下,使用client4 go驱动程序使用个人访问令牌登录的正式方法是什么?

go

我们正在运行两个最重要的服务器。

我们有一个使用https://github.com/Vaelor/python-mattermost-
driver通过社区Python驱动程序使用个人访问令牌登录的python进程。此过程的会话不会超时,这是使用个人访问令牌登录的好处之一。
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驱动程序使用个人访问令牌登录的正式方法是什么?


阅读 294

收藏
2020-07-02

共1个答案

小编典典

关键是 登录,设置个人访问令牌并调用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
}
2020-07-02