我同意@scottwtang, 你会得到未经授权如果您的应用程序没有所需的角色和权限,则会出错。
我试图在我的环境中重现相同的结果并得到以下结果:
我用了下面的脚本从您之前的问题生成证书:
$CN = "GraphApp"
$cert=New-SelfSignedCertificate -Subject "CN=$CN" -CertStoreLocation "Cert:CurrentUserMy" -KeyExportPolicy Exportable -KeySpec Signature -NotAfter (Get-Date).AddYears(5)
$Thumbprint = $Cert.Thumbprint
Get-ChildItem Cert:CurrentUsermy$Thumbprint | Export-Certificate -FilePath $env:USERPROFILEDownloadsGraphApp.cer
Write-Output "$Thumbprint <- Copy/paste this (save it)"
输出:
现在我上传了这个证书到 Azure AD 应用程序,如下所示:
为了$organization参数,您需要传递您的域名。你可以在这里找到:
转到 Azure 门户 -> Azure Active Directory -> 概述 -> 主域
当我运行以下脚本进行连接时在线交流, 我有Access denied像这样的错误:
$clientId="47xxxd8-8x2x-4xxx-bxx7-30cxxxxx8"
$thumbPrint="E4A0F6C6B85EBFxxxxxCD91B5803F88E5"
$organization="xxxxxxxx.onmicrosoft.com"
Connect-ExchangeOnline -AppId $clientId -CertificateThumbprint $thumbPrint -Organization $organization
输出:
至解决错误,您需要添加API permission和Directory对您的应用程序的作用:
确保授予管理员同意添加权限如下:
现在我添加了交易所管理员我的应用程序的角色如下:
转到 Azure 门户 -> Azure Active Directory -> 角色和管理员 -> Exchange 管理员 -> 添加分配
可能需要一会儿成功分配角色如下:
现在我连接到在线交流通过再次运行脚本并运行示例命令Get-EXOMailbox -PropertySets Archive 来验证它并得到回复成功如下:
$clientId="47xxxd8-8x2x-4xxx-bxx7-30cxxxxx8"
$thumbPrint="E4A0F6C6B85EBFxxxxxCD91B5803F88E5"
$organization="xxxxxxxx.onmicrosoft.com"
Connect-ExchangeOnline -AppId $clientId -CertificateThumbprint $thumbPrint -Organization $organization
输出:
因此,请确保分配 required角色和权限让您的应用程序修复错误。