登录
首页 >  Golang >  Go问答

问题解决:msgraph-sdk-go 训练示例代码时获取访问令牌为空的错误解决方法

来源:stackoverflow

时间:2024-02-11 08:18:22 481浏览 收藏

Golang小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《问题解决:msgraph-sdk-go 训练示例代码时获取访问令牌为空的错误解决方法》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!


问题内容

尝试从此处运行 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 <nil>
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 <nil>

正确答案


好吧,经过反复试验后,对我有用的修复是示例中的版本与我正在尝试的实际应用程序不匹配。 我使用的 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 训练示例代码时获取访问令牌为空的错误解决方法》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>