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

Golangisidealforbuildingscalablesystemsduetoitsefficiencyandconcurrency,whilePythonexcelsinquickscriptinganddataanalysisduetoitssimplicityandvastecosystem.Golang'sdesignencouragesclean,readablecodeanditsgoroutinesenableefficientconcurrentoperations,t

Golang在並發性上優於C ,而C 在原始速度上優於Golang。 1)Golang通過goroutine和channel實現高效並發,適合處理大量並發任務。 2)C 通過編譯器優化和標準庫,提供接近硬件的高性能,適合需要極致優化的應用。

選擇Golang的原因包括:1)高並發性能,2)靜態類型系統,3)垃圾回收機制,4)豐富的標準庫和生態系統,這些特性使其成為開發高效、可靠軟件的理想選擇。

Golang適合快速開發和並發場景,C 適用於需要極致性能和低級控制的場景。 1)Golang通過垃圾回收和並發機制提升性能,適合高並發Web服務開發。 2)C 通過手動內存管理和編譯器優化達到極致性能,適用於嵌入式系統開發。

Golang在編譯時間和並發處理上表現更好,而C 在運行速度和內存管理上更具優勢。 1.Golang編譯速度快,適合快速開發。 2.C 運行速度快,適合性能關鍵應用。 3.Golang並發處理簡單高效,適用於並發編程。 4.C 手動內存管理提供更高性能,但增加開發複雜度。

Golang在Web服務和系統編程中的應用主要體現在其簡潔、高效和並發性上。 1)在Web服務中,Golang通過強大的HTTP庫和並發處理能力,支持創建高性能的Web應用和API。 2)在系統編程中,Golang利用接近硬件的特性和對C語言的兼容性,適用於操作系統開發和嵌入式系統。

Golang和C 在性能對比中各有優劣:1.Golang適合高並發和快速開發,但垃圾回收可能影響性能;2.C 提供更高性能和硬件控制,但開發複雜度高。選擇時需綜合考慮項目需求和團隊技能。

Golang适合高性能和并发编程场景,Python适合快速开发和数据处理。1.Golang强调简洁和高效,适用于后端服务和微服务。2.Python以简洁语法和丰富库著称,适用于数据科学和机器学习。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

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