首頁  >  文章  >  後端開發  >  執行 msgraph-sdk-go 訓練範例程式碼時出現「取得存取權杖為空」錯誤

執行 msgraph-sdk-go 訓練範例程式碼時出現「取得存取權杖為空」錯誤

WBOY
WBOY轉載
2024-02-09 08:12:30614瀏覽

运行 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 <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 檔案以使用v0.48。 com/microsoftgraph/msgraph-training-go" rel="nofollow noreferrer">儲存庫 一切開始工作。 希望這對其他人以後有幫助。

以上是執行 msgraph-sdk-go 訓練範例程式碼時出現「取得存取權杖為空」錯誤的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除