使用 curl 和 accestoken 从 Azure AD 获取信息

Get information out of Azure AD with curl and accestoken

重新发布警告,但现在用 curl(Ubuntu 提示和 shell 脚本)、Azure Active Directory (AAD)、承载令牌、传递参数和引号苦苦挣扎了几个小时我无法在网上找到的所有帮助下解决这个问题。

我得到了一个 accesToken = { 一个非常长的不可用且经常需要刷新的字符串}

1) 当我执行时:

response=curl --location --request GET 'https://graph.microsoft.com/v1.0/users?$select=userPrincipalName' --header "Authorization: Bearer { this long string from above }"

我收到了一个正确的答案,我在 AAD 中拥有的所有用户的 userPrincipalNames 列表。

2) 当我执行下面提到的任何命令时:

export authentication="Authorization: Bearer "$accessToken

response=curl --location --request GET 'https://graph.microsoft.com/v1.0/users?$select=userPrincipalName' --header "'"$authentication"'"

response=curl --location --request GET 'https://graph.microsoft.com/v1.0/users?$select=userPrincipalName' --header ""$authentication""

response=curl --location --request GET 'https://graph.microsoft.com/v1.0/users?$select=userPrincipalName' --header "$authentication"

response=curl --location --request GET "https://graph.microsoft.com/v1.0/users?$select=userPrincipalName" --header $authentication

我得到了一个 "can not resolve host error" 或类似 '}date": "2020-03-31T15:26:58"40e8-a128-67f994881632",' ...

3) 当我执行这个命令时:

response=curl --location --request GET "https://graph.microsoft.com/v1.0/users?$select=userPrincipalName" --header "Authorization: Bearer $accessToken"

我收到了 AAD 中列出的所有用户的所有信息,而不仅仅是 url 中提到的 userPrincipalName。

我主要且仅有的两个问题是:

1) 关于引号和

我到底错了什么

2) 如何只获取此 userPrincipalName,而无需在我的命令中输入这么长的 accesToken?

在此先感谢您的建议和思考以及亲切的问候,

阿德·戴克斯曼

1) what exactly do I wrong regarding quotes

我在我的网站上进行了测试,它使用如下代码工作:

export accessToken="eyJoexxxxxxxxxxxxxxxx"
export authentication="Authorization: Bearer "$accessToken
curl --location --request GET 'https://graph.microsoft.com/v1.0/users' --header "$authentication"

整个命令截图如下:

2) How can I get this userPrincipalName only, without typing this long accesToken in my command?

不,不可能。当您想在 Azure AD 中访问 Graph API 时,您必须包含您的访问令牌。