Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Azure AD Go SDK守护程序应用程序列表用户返回;访问令牌丢失或格式不正确;_Azure_Go_Azure Active Directory_Azure Ad Graph Api - Fatal编程技术网

Azure AD Go SDK守护程序应用程序列表用户返回;访问令牌丢失或格式不正确;

Azure AD Go SDK守护程序应用程序列表用户返回;访问令牌丢失或格式不正确;,azure,go,azure-active-directory,azure-ad-graph-api,Azure,Go,Azure Active Directory,Azure Ad Graph Api,我试图通过graph 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={cl

我试图通过graph 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执行此操作时,它失败了

我已按照以下要求设置认证所需的环境变量:

代码

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
包处理

更新1

我通过设置
AZURE\u GO\u SDK\u LOG\u LEVEL=debug
运行它的调试模式,发现
GET
请求URL与我在
curl
命令中使用的URL不同:

(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广告图api显示如下:
https://graph.windows.net/{tenant_id}/users?api版本=1.6

Microsoft graph api显示如下内容:
https://graph.microsoft.com/v1.0/users

因此,您需要为在azure ad中注册的应用程序添加azure ad graph权限,但不需要添加microsoft graph权限。请按照以下步骤添加权限:

1。转到azure广告中的应用程序,单击“API权限”->“添加权限”->“azure Active Directory图表”。

2.添加“目录”权限。


3。别忘了授予管理员许可。

sdk似乎在后端使用azure ad graph api,而不是microsoft graph api

Azure广告图api显示如下:
https://graph.windows.net/{tenant_id}/users?api版本=1.6

Microsoft graph api显示如下内容:
https://graph.microsoft.com/v1.0/users

因此,您需要为在azure ad中注册的应用程序添加azure ad graph权限,但不需要添加microsoft graph权限。请按照以下步骤添加权限:

1。转到azure广告中的应用程序,单击“API权限”->“添加权限”->“azure Active Directory图表”。

2.添加“目录”权限。


3.别忘了授予管理员许可。

虽然MSAL尚未就绪(在SDK中),但ADAL似乎已经被弃用

Azure控制台


这意味着答案仍然有效。

尽管MSAL尚未就绪(在SDK中),但ADAL似乎已经被弃用

Azure控制台


这意味着答案仍然有效。

感谢您提供所有这些详细信息!我会给它一个镜头,并确认。这是正确的,因为谢谢你给所有这些细节!我会试一试并确认。这是正确的
{"odata.error":{"code":"Authentication_ExpiredToken","message":{"lang":"en","value":"Your access token has expired. Please renew it before submitting the request."}}}%