无法使用PowerShell中的访问令牌登录

huangapple go评论58阅读模式
英文:

Could not login using access token from powershell

问题

我正在使用刷新令牌获取访问令牌,然后尝试连接到ExchangeOnlineAzureAD,如下所示:

$clientId = "7b2cd291-87e9-49fc-888e-xxxxxxxxxxxx"
$clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$tenantId = "baf62cb4-4cc6-4af9-a3c1-xxxxxxxxxxxx"
$refreshToken = "xxxxxxxx..."
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/x-www-form-urlencoded")

$body = "client_id=" + $clientId + "&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&refresh_token=" + $refreshToken + "&grant_type=refresh_token&client_secret=" + $clientSecret

$url = "https://login.microsoftonline.com/" + $tenantId + "/oauth2/v2.0/token"
$response = Invoke-RestMethod $url -Method 'POST' -Headers $headers -Body $body

Connect-ExchangeOnline -UserPrincipalName divyesh@myorg.com -AccessToken $response.access_token
Connect-AzAccount -AccessToken $response.access_token -AccountId 'divyesh@myorg.com'

我成功获取了访问令牌,但无法连接到ExchangeOnlineAzureAD,出现以下错误。对于ExchangeOnline

未授权
在 C:\Program Files\WindowsPowerShell\Modules\ExchangeOnlineManagement.1.0\netFramework\ExchangeOnlineManagement.psm1:733 字符:21
+                     throw $_.Exception;
+                     ~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [], UnauthorizedAccessException
+ FullyQualifiedErrorId : UnAuthorized

对于AzureAD

警告: 无法获取租户'organizations'的令牌,错误为'身份验证失败。'
Connect-AzAccount : 身份验证失败。
在第16行字符1
+ Connect-AzAccount -AccessToken $response.access_token -AccountId 'div ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : CloseError: (:) [Connect-AzAccount], CloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.ConnectAzureRmAccountCommand

我不确定出了什么问题,请帮我解决这个问题。

英文:

I am using the refresh token to get an access token and then trying to connect to ExchangeOnline and AzureAD like

$clientId = "7b2cd291-87e9-49fc-888e-xxxxxxxxxxxx"
$clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$tenantId = "baf62cb4-4cc6-4af9-a3c1-xxxxxxxxxxxx"
$refreshToken = "xxxxxxxx..."
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/x-www-form-urlencoded")

$body = "client_id=" + $clientId + "&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&refresh_token=" + $refreshToken + "&grant_type=refresh_token&client_secret=" + $clientSecret

$url = "https://login.microsoftonline.com/" + $tenantId + "/oauth2/v2.0/token"
$response = Invoke-RestMethod $url -Method 'POST' -Headers $headers -Body $body

Connect-ExchangeOnline -UserPrincipalName divyesh@myorg.com -AccessToken $response.access_token
Connect-AzAccount -AccessToken $response.access_token -AccountId 'divyesh@myorg.com'

I am getting access token successfully but could not able to connect to ExchangeOnline and AzureAD. It is giving me errors like the below for ExchangeOnline.

UnAuthorized
At C:\Program Files\WindowsPowerShell\Modules\ExchangeOnlineManagement.1.0\netFramework\ExchangeOnlineManagement.psm1:733 char:21
+                     throw $_.Exception;
+                     ~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [], UnauthorizedAccessException
+ FullyQualifiedErrorId : UnAuthorized

and for AzureAD

WARNING: Unable to acquire a token for tenant 'organizations' with error 'Authentication failed.'
Connect-AzAccount : Authentication failed.
At line:16 char:1
+ Connect-AzAccount -AccessToken $response.access_token -AccountId 'div ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : CloseError: (:) [Connect-AzAccount], CloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.ConnectAzureRmAccountCommand

I am not sure what is wrong here?

答案1

得分: 1

根据访问令牌的类型,您需要在 Connect-ExchangeOnline 命令中使用 AccessToken 参数和 Organization 参数。

您已经在 Azure Active Directory 中为应用程序配置了应用程序权限,并且有一个用于守护程序/后台服务的访问令牌,因此您不能将 AccessToken 参数与 UserPrincipalName 参数结合使用。

步骤:

生成具有正确范围的访问令牌

生成访问令牌时,您使用的范围是 https://graph.microsoft.com/.default,但正确的应该是 https://outlook.office365.com/.default

要生成访问令牌,请使用以下脚本:

$clientId = "7b2cd291-87e9-49fc-888e-xxxxxxxxxxxx"
$clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$tenantId = "baf62cb4-4cc6-4af9-a3c1-xxxxxxxxxxxx"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/x-www-form-urlencoded")
$body = "client_id=" + $clientId + "&scope=https%3A%2F%2Foutlook.office365.com%2F.default&grant_type=client_credentials&client_secret=" + $clientSecret

$url = "https://login.microsoftonline.com/" + $tenantId + "/oauth2/v2.0/token"
$response = Invoke-RestMethod $url -Method 'POST' -Headers $headers -Body $body

连接

使用 OrganizationAccessToken 参数进行连接:

Connect-ExchangeOnline -Organization '<your_domain_name>.onmicrosoft.com' -AccessToken $response.access_token

以上步骤假设您已经为您的应用程序分配了正确的 Azure 角色并修改了清单

英文:

Depending on the type of access token, you need to use AccessToken parameter with the Organization parameter.

You've configured application permissions for the app in Azure Active Directory and have an access token for a daemon/background service, so you cannot combine AccessToken parameter with UserPrincipalName

Connect-ExchangeOnline -UserPrincipalName xxx -AccessToken yyy

Steps:

Generate access token with the correct scope

When generating access token you are using scope https://graph.microsoft.com/.default but the correct one should be https://outlook.office365.com/.default

To generate access token use this script

$clientId = &quot;7b2cd291-87e9-49fc-888e-xxxxxxxxxxxx&quot;
$clientSecret = &quot;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&quot;
$tenantId = &quot;baf62cb4-4cc6-4af9-a3c1-xxxxxxxxxxxx&quot;
$headers = New-Object &quot;System.Collections.Generic.Dictionary[[String],[String]]&quot;
$headers.Add(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;)
$body = &quot;client_id=&quot; + $clientId + &quot;&amp;scope=https%3A%2F%2Foutlook.office365.com%2F.default&amp;grant_type=client_credentials&amp;client_secret=&quot; + $clientSecret

$url = &quot;https://login.microsoftonline.com/&quot; + $tenantId + &quot;/oauth2/v2.0/token&quot;
$response = Invoke-RestMethod $url -Method &#39;POST&#39; -Headers $headers -Body $body

Connect

Use Organization and AccessToken parameters for connecting

Connect-ExchangeOnline -Organization &#39;&lt;your_domain_name&gt;.onmicrosoft.com&#39; -AccessToken $response.access_token

The steps above suppose that you've assigned the correct Azure role to your application and modified the manifest.

huangapple
  • 本文由 发表于 2023年6月12日 16:38:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定