Azure AD Go SDK 守护程序应用程序列表用户返回 "Access Token missing or malformed"

标签 azure go azure-active-directory azure-ad-graph-api

我正在尝试通过图形 API Go SDK 检索用户详细信息。我有一个daemon application它已设置了足够的权限,我已通过 curl 验证了该权限,如下所示:

获取 token

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."}}}]

文档 here对我来说,身份验证和 token 由 auth 包处理。

更新1

我通过设置 AZURE_GO_SDK_LOG_LEVEL=DEBUG 运行 Debug模式,发现 GET 请求 URL 与我在 curl 中使用的不同命令:

(2020-06-16T15:31:49.3790420+10:00) INFO: REQUEST: GET https://graph.windows.net/{tenant_id}/users?api-version=1.6
User-Agent: Go/go1.13.11 (amd64-darwin) go-autorest/v14.1.1 Azure-SDK-For-Go/v43.2.0 graphrbac/1.6
Authorization: **REDACTED**
(2020-06-16T15:31:50.5191120+10:00) INFO: RESPONSE: 401 https://graph.windows.net/{tenant_id}/users?api-version=1.6

如果我在 curl 命令中使用该 URL,我会得到:

{"odata.error":{"code":"Authentication_ExpiredToken","message":{"lang":"en","value":"Your access token has expired. Please renew it before submitting the request."}}}%

最佳答案

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 广告中注册的应用程序添加 Azure 广告图形权限,但不添加 Microsoft 图形权限。请按照以下步骤添加权限:

1. 转到 Azure 广告中的应用程序,然后点击“API 权限”-->“添加权限”-->“Azure Active Directory 图形”。 enter image description here

2.添加“目录”权限。 enter image description here

3.不要忘记授予管理员同意。

关于Azure AD Go SDK 守护程序应用程序列表用户返回 "Access Token missing or malformed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62400615/

相关文章:

azure - Microsoft 帐户开发人员中心/应用程序注册门户中的单个应用程序注册

azure - 如何更改 sidecarject-config.yaml 中的 istio 全局参数

go - 如何通过 utc 偏移量加载位置

angular - 获取 OAuth token 以供 Azure DevOps API 使用

azure - 如何制作适用于 Office 365 的 Google Apps iDP

go - Go Web 服务器在哪里查找文件

string - 如何将通配符 * 集成到数学验证路由中

azure - 有人可以解释一下 Azure AD 应用程序访问和刷新 token 的超时和验证吗?

sql-server - 突触外部表 "Unauthorized"

c# - 使用 C# 的 Azure 文本分析给出错误