我正在嘗試通過圖形 API Go SDK 檢索用戶詳細信息。我有一個守護程序應用程序,它已設置有足夠的權(quán)限,我已通過curl如下所示驗證:獲取令牌curl \ -X POST \ -H "Content-Type: application/x-www-form-urlencoded" \ --data 'client_id={client_id}&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret={client_secret}&grant_type=client_credentials' \https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token要求curl -X GET \ -H "Authorization: Bearer XYZ...." \"https://graph.microsoft.com/v1.0/users"我成功獲得了用戶列表。但是,當我通過 Go SDK 嘗試此操作時,它失敗了。我已經(jīng)按照https://github.com/Azure/azure-sdk-for-go#more-authentication-details設置了身份驗證所需的環(huán)境變量:- `AZURE_TENANT_ID`: Specifies the Tenant to which to authenticate.- `AZURE_CLIENT_ID`: Specifies the app client ID to use.- `AZURE_CLIENT_SECRET`: Specifies the app secret to use代碼func main() { authorizer, err := auth.NewAuthorizerFromEnvironment() if err != nil { fmt.Println(err) } client := graphrbac.NewUsersClient(os.Getenv("AZURE_TENANT_ID")) client.Authorizer = authorizer if _, err := client.List(context.Background(), "", ""); err != nil { fmt.Println("list users", err) }}錯誤list users graphrbac.UsersClient#List: Failure responding to request: StatusCode=401 -- Original Error: autorest/azure: Service returned an error. Status=401 Code="Unknown" Message="Unknown service error" Details=[{"odata.error":{"code":"Authentication_MissingOrMalformed","message":{"lang":"en","value":"Access Token missing or malformed."}}}]這里給我的文檔建議身份驗證和令牌由auth包處理。
2 回答

慕尼黑的夜晚無繁華
TA貢獻1864條經(jīng)驗 獲得超6個贊
似乎 sdk 在后端使用 azure ad graph api 但不是 microsoft graph api。
Azure AD 圖形 api 顯示如下:https://graph.windows.net/{tenant_id}/users?api-version=1.6
Microsoft graph api 顯示如下:https://graph.microsoft.com/v1.0/users
因此,您需要為您的 azure ad 中注冊的應用程序添加 azure ad graph 權(quán)限,而不是添加 microsoft graph 權(quán)限。請按照以下步驟添加權(quán)限:
1.在您的 azure 廣告中轉(zhuǎn)到您的應用程序,然后單擊“API 權(quán)限”-->“添加權(quán)限”-->“Azure Active Directory Graph”。
2.添加“目錄”權(quán)限。
3.不要忘記授予管理員同意。

MM們
TA貢獻1886條經(jīng)驗 獲得超2個贊
- 2 回答
- 0 關(guān)注
- 141 瀏覽
添加回答
舉報
0/150
提交
取消