英文:
Could not login using access token from powershell
问题
我正在使用刷新令牌获取访问令牌,然后尝试连接到ExchangeOnline和AzureAD,如下所示:
$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'
我成功获取了访问令牌,但无法连接到ExchangeOnline和AzureAD,出现以下错误。对于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
连接
使用 Organization
和 AccessToken
参数进行连接:
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 = "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
Connect
Use Organization
and AccessToken
parameters for connecting
Connect-ExchangeOnline -Organization '<your_domain_name>.onmicrosoft.com' -AccessToken $response.access_token
The steps above suppose that you've assigned the correct Azure role to your application and modified the manifest.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论