iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >运行 msgraph-sdk-go 训练示例代码时出现“获取访问令牌为空”错误
  • 338
分享到

运行 msgraph-sdk-go 训练示例代码时出现“获取访问令牌为空”错误

2024-02-09 07:02:04 338人浏览 独家记忆
摘要

PHP小编小新今天为大家带来了一个关于msgraph-sdk-Go训练示例代码的问题。在运行过程中,可能会遇到“获取访问令牌为空”错误。这个错误可能导致代码无法正确执行,影响训练结果。

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 out
func (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 API
code: 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 go.mod 文件以使用 v0.48。 com/microsoftgraph/msgraph-training-go" rel="nofollow noreferrer">存储库 一切开始工作。 希望这对其他人以后有帮助。

以上就是运行 msgraph-sdk-go 训练示例代码时出现“获取访问令牌为空”错误的详细内容,更多请关注编程网其它相关文章!

--结束END--

本文标题: 运行 msgraph-sdk-go 训练示例代码时出现“获取访问令牌为空”错误

本文链接: https://www.lsjlt.com/news/562293.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作