php小编小新今天为大家带来了一个关于msgraph-sdk-go训练示例代码的问题。在运行过程中,可能会遇到“获取访问令牌为空”错误。这个错误可能导致代码无法正确执行,影响训练结果。在本文中,我们将详细介绍这个问题的原因和解决方法,帮助大家顺利运行示例代码,享受到更好的训练体验。
问题内容
尝试从此处运行 msgraph-sdk-go 训练代码时:https://github.com/microsoftgraph/msgraph-training-go,我收到 invalidauthenticationtokenmsg:执行图形 api 调用时访问令牌为空 。我配置了一个带有即时沙箱的 microsoft 开发人员帐户以供试用。我按照此处的教程中所述创建了应用程序注册,并授予了该应用程序所需的权限。该代码能够获取 apptoken,但对于获取 users 的调用失败,并出现上述错误。我在这里遗漏了什么吗?
我尝试了以下 msgraph-training 示例中的代码
func (g *graphhelper) initializegraphforappauth() error { clientid := os.getenv("client_id") tenantid := os.getenv("tenant_id") clientsecret := os.getenv("client_secret") credential, err := azidentity.newclientsecretcredential(tenantid, clientid, clientsecret, nil) if err != nil { return err } g.clientsecretcredential = credential // create an auth provider using the credential authprovider, err := auth.newazureidentityauthenticationproviderwithscopes(g.clientsecretcredential, []string{ "https://graph.microsoft.com/.default", }) if err != nil { return err } // create a request adapter using the auth provider adapter, err := msgraphsdk.newgraphrequestadapter(authprovider) if err != nil { return err } // create a graph client using request adapter client := msgraphsdk.newgraphserviceclient(adapter) g.appclient = client return nil}// this part works, and i get the apptoken with required scope, once decoded.func (g *graphhelper) getapptoken() (*string, error) { token, err := g.clientsecretcredential.gettoken(context.background(), policy.tokenrequestoptions{ scopes: []string{ "https://graph.microsoft.com/.default", }, }) if err != nil { return nil, err } fmt.println("expires on : ", token.expireson) return &token.token, nil}// the getusers function errors outfunc (g *graphhelper) getusers() (models.usercollectionresponseable, error) { var topvalue int32 = 25 query := users.usersrequestbuildergetqueryparameters{ // only request specific properties select: []string{"displayname", "id", "mail"}, // get at most 25 results top: &topvalue, // sort by display name orderby: []string{"displayname"}, } resp, err := g.appclient.users(). get(context.background(), &users.usersrequestbuildergetrequestconfiguration{ queryparameters: &query, }) if err != nil { fmt.println("users.get got error", err.error(), resp) printodataerror(err) } resp, err = g.appclient.users(). get(context.background(), nil) if err != nil { fmt.println("users.get got error with nil", err.error(), resp) } return resp, err}
登录后复制
我已按照教程中所述在应用程序中添加了 user.read.all 权限。我没有获取用户列表,而是收到以下错误:
Users.Get got Error error status code received from the API error: error status code received from the APIcode: InvalidAuthenticationTokenmsg: Access token is empty.Users.Get got Error with nil error status code received from the API
登录后复制
解决方法
好吧,经过反复试验后,对我有用的修复是示例中的版本与我正在尝试的实际应用程序不匹配。我使用的 beta msgraph 应用程序的版本是 v0.49,而 msgraphsdk 教程使用的是 v0.48。 go mod 命令最初选择了最新的 v0.49,我猜,在查看 msgraph-training 存储库 一切开始工作。希望这对其他人以后有帮助。
以上就是运行 msgraph-sdk-go 训练示例代码时出现“获取访问令牌为空”错误的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2484871.html