執行 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中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

OpenSSL,作為廣泛應用於安全通信的開源庫,提供了加密算法、密鑰和證書管理等功能。然而,其歷史版本中存在一些已知安全漏洞,其中一些危害極大。本文將重點介紹Debian系統中OpenSSL的常見漏洞及應對措施。 DebianOpenSSL已知漏洞:OpenSSL曾出現過多個嚴重漏洞,例如:心臟出血漏洞(CVE-2014-0160):該漏洞影響OpenSSL1.0.1至1.0.1f以及1.0.2至1.0.2beta版本。攻擊者可利用此漏洞未經授權讀取服務器上的敏感信息,包括加密密鑰等。

後端學習路徑:從前端轉型到後端的探索之旅作為一名從前端開發轉型的後端初學者,你已經有了nodejs的基礎,...

Go爬蟲Colly中的Queue線程問題探討在使用Go語言的Colly爬蟲庫時,開發者常常會遇到關於線程和請求隊列的問題。 �...

Go語言中用於浮點數運算的庫介紹在Go語言(也稱為Golang)中,進行浮點數的加減乘除運算時,如何確保精度是�...

在BeegoORM框架下,如何指定模型關聯的數據庫?許多Beego項目需要同時操作多個數據庫。當使用Beego...

Go語言中使用RedisStream實現消息隊列時類型轉換問題在使用Go語言與Redis...

Go語言中字符串打印的區別:使用Println與string()函數的效果差異在Go...

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...
