我正在尝试通过图形 API Go SDK 检索用户详细信息。我有一个守护程序应用程序,它已设置有足够的权限,我已通过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 尝试此操作时,它失败了。
我已经按照https://github.com/Azure/azure-sdk-for-go#more-authentication-details设置了身份验证所需的环境变量:
- `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包处理。
慕尼黑的夜晚无繁华
MM们
相关分类