dongmin1166 2018-08-10 23:26
浏览 28
已采纳

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

We're running two mattermost servers.

One we have a python process logging in with https://github.com/Vaelor/python-mattermost-driver using a personal access token with the community Python driver. This process has a session that doesn't time out which is one of the benefits of using a personal access token to login. https://docs.mattermost.com/developer/personal-access-tokens.html .

We log in using a username and password with the client4 go driver and this works however it times out after a while. It appears there is no way of using a personal access token to log in with the official client 4 driver.

The documentation for the Mattermost Client4 code is at https://godoc.org/github.com/mattermost/platform/model#Client

The source for client4 is at https://github.com/mattermost/mattermost-server/blob/master/model/client4.go

The closest thing that looks like it would work is logging in under with a username and password and then setting the authentication token via client.MockSession which failed on testing.

What's the official way of logging in with a personal access token using the client4 go driver for mattermost?

  • 写回答

1条回答 默认 最新

  • dtttlua7165 2018-08-12 03:45
    关注

    The key is not logging in, setting the personal access token and calling client.GetMe. This became clear after looking through the python driver source code. The relevant fragment is.

        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
        }
    

    Context.

    /* 
        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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比