为了账号安全,请及时绑定邮箱和手机立即绑定

如何从以下 golang 函数生成具有 TTL 8h 的令牌?

如何从以下 golang 函数生成具有 TTL 8h 的令牌?

Go
湖上湖 2022-10-24 15:49:58
我是编程新手,不知道在我的客户端 golang 程序的源代码中使用令牌生成客户端 api 函数。寻求一些建议。太感谢了。源码包:https ://pkg.go.dev/github.com/gravitational/teleport/api/client#Client.UpsertToken函数源代码:func (c *Client) UpsertToken(ctx context.Context, token types.ProvisionToken) error {    tokenV2, ok := token.(*types.ProvisionTokenV2)    if !ok {        return trace.BadParameter("invalid type %T", token)    }    _, err := c.grpc.UpsertToken(ctx, tokenV2, c.callOpts...)    return trail.FromGRPC(err)}My code:package mainimport (    "context"    "crypto/tls"    "fmt"    "log"    "os"    "strings"    "time"    "github.com/gravitational/teleport/api/client"    "github.com/gravitational/teleport/api/client/proto"    "google.golang.org/grpc")// Client is a gRPC Client that connects to a Teleport Auth server either// locally or over ssh through a Teleport web proxy or tunnel proxy.//// This client can be used to cover a variety of Teleport use cases,// such as programmatically handling access requests, integrating// with external tools, or dynamically configuring Teleport.type Client struct {    // c contains configuration values for the client.    //c Config    // tlsConfig is the *tls.Config for a successfully connected client.    tlsConfig *tls.Config    // dialer is the ContextDialer for a successfully connected client.    //dialer ContextDialer    // conn is a grpc connection to the auth server.    conn *grpc.ClientConn    // grpc is the gRPC client specification for the auth server.    grpc proto.AuthServiceClient    // closedFlag is set to indicate that the connnection is closed.    // It's a pointer to allow the Client struct to be copied.    closedFlag *int32    // callOpts configure calls made by this client.    callOpts []grpc.CallOption}/*type ProvisionToken interface {    Resource    // SetMetadata sets resource metatada    SetMetadata(meta Metadata)    // GetRoles returns a list of teleport roles    // that will be granted to the user of the token    // in the crendentials}输出:语法错误而不是创建令牌
查看完整描述

1 回答

?
守候你守候我

TA贡献1802条经验 获得超10个赞

看来您的代码有很多错误。而且,很明显您遇到了语法错误。我相信您会在控制台中获得实际发生这些语法错误的行号。


请理解 Golang 的语法以及如何调用函数以及我应该向这些函数传递多少参数。


在查看您的代码后,我想指出几个错误。


//It shouldn't be like this

ctx, err, token, err2 := clt.UpsertToken(ctx, token)


//Instead it should be like this

 err := clt.UpsertToken(ctx, token)

//The return type of UpsertToken() method is error, you should use only one variable to receive this error.

strings.Contains()函数有两个参数,但你传递了四个。

请参阅此文档以获取string.Contains()

您正在使用循环内的条件和从不分配t := 0和检查它。ifforincremented

请参阅本文档以获取fmt.Printf()

参考这个功能

删除所有语法错误,然后只有您的代码将运行并交叉检查您的逻辑。

如果您想查看语法错误示例,请在此处查看:https ://go.dev/play/p/Hhu48UqlPRF


查看完整回答
反对 回复 2022-10-24
  • 1 回答
  • 0 关注
  • 109 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号