Business Central Admin API-可以删除/停止会话

发布于 2025-01-19 07:18:01 字数 4691 浏览 4 评论 0 原文

Using powershell, I can connect to the business central admin API with a registered app on Azure as per the following setup

# This sample authenticates to Azure Active Directory (AAD) an obtains an access token.
# The access token can be used for authenticating to Business Central APIs.
Get-Module
import-Module AzureAD

# Parameters
$aadAppId = "ea954743-####-4025-bc90-############"        # AAD app id
$aadAppRedirectUri = "http://localhost"                   # AAD app redirect URI
$aadTenantId = "46fe5ca5-####-4e42-92e9-############"     # tenant id


# Load Microsoft.Identity.Client.dll
Add-Type -Path "C:\Program Files\WindowsPowerShell\Modules\AzureAD\2.0.2.135\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"

# Get access token
$ctx = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]::new("https://login.microsoftonline.com/$aadTenantId")
$redirectUri = New-Object -TypeName System.Uri -ArgumentList $aadAppRedirectUri
$platformParameters = New-Object -TypeName Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters -ArgumentList ([Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior]::Always)
$accessToken = $ctx.AcquireTokenAsync("https://api.businesscentral.dynamics.com", $aadAppId, $redirectUri, $platformParameters).GetAwaiter().GetResult().AccessToken
Write-Host -ForegroundColor Cyan 'Authentication complete - we have an access token for Business Central, and it is stored in the $accessToken variable.'

#Get list of environments
$response = Invoke-WebRequest `
    -Method Get `
    -Uri    "https://api.businesscentral.dynamics.com/admin/v2.11/applications/businesscentral/environments" `
    -Headers @{Authorization=("Bearer $accessToken")}
Write-Host (ConvertTo-Json (ConvertFrom-Json $response.Content))

#List user sessions
$environmentName = "TEST"
$response = Invoke-WebRequest `
    -Method Get `
    -Uri    "https://api.businesscentral.dynamics.com/admin/v2.11/applications/businesscentral/environments/$environmentName/sessions" `
    -Headers @{Authorization=("Bearer $accessToken")}
Write-Host (ConvertTo-Json (ConvertFrom-Json $response.Content))

#Get Session Details
$sessionId = "145197"
$response = Invoke-WebRequest `
    -Method Get `
    -Uri    "https://api.businesscentral.dynamics.com/admin/v2.11/applications/businesscentral/environments/$environmentName/sessions/$sessionId" `
    -Headers @{Authorization=("Bearer $accessToken")}
Write-Host (ConvertTo-Json (ConvertFrom-Json $response.Content))

#Stop/Delete Session
$response = Invoke-WebRequest `
    -Method Delete `
    -Uri    "https://api.businesscentral.dynamics.com/admin/v2.11/applications/businesscentral/environments/$environmentName/sessions/$sessionId" `
    -Headers @{Authorization=("Bearer $accessToken")}
Write-Host (ConvertTo-Json (ConvertFrom-Json $response.Content))

$response = Invoke-WebRequest `
    -Method Delete `
    -Uri    "https://api.businesscentral.dynamics.com/admin/v2.11/applications/businesscentral/environments/$environmentName/sessions/$sessionId" `
    -Headers @{Authorization=("Bearer $accessToken")}
Write-Host "Responded with: $($response.StatusCode) $($response.StatusDescription)"

当我尝试删除会话时,它会产生以下错误。

PS C:\WINDOWS\system32> #Stop/Delete Session
$response = Invoke-WebRequest `
    -Method Delete `
    -Uri    "https://api.businesscentral.dynamics.com/admin/v2.11/applications/businesscentral/environments/$environmentName/sessions/$sessionId" `
    -Headers @{Authorization=("Bearer $accessToken")}
Write-Host (ConvertTo-Json (ConvertFrom-Json $response.Content))
ConvertFrom-Json : Cannot convert 'System.Byte[]' to the type 'System.String' required by parameter 
'InputObject'. Specified method is not supported.
At line:6 char:46
+ Write-Host (ConvertTo-Json (ConvertFrom-Json $response.Content))
+                                              ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [ConvertFrom-Json], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.ConvertFromJsonComm 
   and
 

PS C:\WINDOWS\system32> $response = Invoke-WebRequest `
    -Method Delete `
    -Uri    "https://api.businesscentral.dynamics.com/admin/v2.11/applications/businesscentral/environments/$environmentName/sessions/$sessionId" `
    -Headers @{Authorization=("Bearer $accessToken")}
Write-Host "Responded with: $($response.StatusCode) $($response.StatusDescription)"

Responded with: 204 No Content

令牌的范围看起来还不错,或者至少与我在Azure应用程序中设置的内容相同,该应用程序是委派权限的。假设这些是正确的权限。

“ SCP”:“ Financials.ReadWrite.All User_Impersonation”,

任何帮助都非常感谢!

Using powershell, I can connect to the business central admin API with a registered app on Azure as per the following setup Business Central Admin API. I am able to retrieve the list of environments and also the list of sessions etc. One of the key requirements when using the API, is to be able to delete/stop sessions. However when I attempt to do this, it is giving me an error which is basically a null response for the delete method.

# This sample authenticates to Azure Active Directory (AAD) an obtains an access token.
# The access token can be used for authenticating to Business Central APIs.
Get-Module
import-Module AzureAD

# Parameters
$aadAppId = "ea954743-####-4025-bc90-############"        # AAD app id
$aadAppRedirectUri = "http://localhost"                   # AAD app redirect URI
$aadTenantId = "46fe5ca5-####-4e42-92e9-############"     # tenant id


# Load Microsoft.Identity.Client.dll
Add-Type -Path "C:\Program Files\WindowsPowerShell\Modules\AzureAD\2.0.2.135\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"

# Get access token
$ctx = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]::new("https://login.microsoftonline.com/$aadTenantId")
$redirectUri = New-Object -TypeName System.Uri -ArgumentList $aadAppRedirectUri
$platformParameters = New-Object -TypeName Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters -ArgumentList ([Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior]::Always)
$accessToken = $ctx.AcquireTokenAsync("https://api.businesscentral.dynamics.com", $aadAppId, $redirectUri, $platformParameters).GetAwaiter().GetResult().AccessToken
Write-Host -ForegroundColor Cyan 'Authentication complete - we have an access token for Business Central, and it is stored in the $accessToken variable.'

#Get list of environments
$response = Invoke-WebRequest `
    -Method Get `
    -Uri    "https://api.businesscentral.dynamics.com/admin/v2.11/applications/businesscentral/environments" `
    -Headers @{Authorization=("Bearer $accessToken")}
Write-Host (ConvertTo-Json (ConvertFrom-Json $response.Content))

#List user sessions
$environmentName = "TEST"
$response = Invoke-WebRequest `
    -Method Get `
    -Uri    "https://api.businesscentral.dynamics.com/admin/v2.11/applications/businesscentral/environments/$environmentName/sessions" `
    -Headers @{Authorization=("Bearer $accessToken")}
Write-Host (ConvertTo-Json (ConvertFrom-Json $response.Content))

#Get Session Details
$sessionId = "145197"
$response = Invoke-WebRequest `
    -Method Get `
    -Uri    "https://api.businesscentral.dynamics.com/admin/v2.11/applications/businesscentral/environments/$environmentName/sessions/$sessionId" `
    -Headers @{Authorization=("Bearer $accessToken")}
Write-Host (ConvertTo-Json (ConvertFrom-Json $response.Content))

#Stop/Delete Session
$response = Invoke-WebRequest `
    -Method Delete `
    -Uri    "https://api.businesscentral.dynamics.com/admin/v2.11/applications/businesscentral/environments/$environmentName/sessions/$sessionId" `
    -Headers @{Authorization=("Bearer $accessToken")}
Write-Host (ConvertTo-Json (ConvertFrom-Json $response.Content))

$response = Invoke-WebRequest `
    -Method Delete `
    -Uri    "https://api.businesscentral.dynamics.com/admin/v2.11/applications/businesscentral/environments/$environmentName/sessions/$sessionId" `
    -Headers @{Authorization=("Bearer $accessToken")}
Write-Host "Responded with: $($response.StatusCode) $($response.StatusDescription)"

When I try to delete a session, it gives the following error.

PS C:\WINDOWS\system32> #Stop/Delete Session
$response = Invoke-WebRequest `
    -Method Delete `
    -Uri    "https://api.businesscentral.dynamics.com/admin/v2.11/applications/businesscentral/environments/$environmentName/sessions/$sessionId" `
    -Headers @{Authorization=("Bearer $accessToken")}
Write-Host (ConvertTo-Json (ConvertFrom-Json $response.Content))
ConvertFrom-Json : Cannot convert 'System.Byte[]' to the type 'System.String' required by parameter 
'InputObject'. Specified method is not supported.
At line:6 char:46
+ Write-Host (ConvertTo-Json (ConvertFrom-Json $response.Content))
+                                              ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [ConvertFrom-Json], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.ConvertFromJsonComm 
   and
 

PS C:\WINDOWS\system32> $response = Invoke-WebRequest `
    -Method Delete `
    -Uri    "https://api.businesscentral.dynamics.com/admin/v2.11/applications/businesscentral/environments/$environmentName/sessions/$sessionId" `
    -Headers @{Authorization=("Bearer $accessToken")}
Write-Host "Responded with: $($response.StatusCode) $($response.StatusDescription)"

Responded with: 204 No Content

The scope of the token looks alright, or at least the same as what I have setup in the Azure app, which is delegated permissions. Assuming these are the correct permissions.

"scp": "Financials.ReadWrite.All user_impersonation",

Any help greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我心暖 2025-01-26 07:18:01

抱歉,事实证明它正在起作用。 a“ 204无内容”表示删除方法成功完成。我的困惑源于期望没有删除方法的响应。

Apologies, it turns out it is working. A '204 No Content' indicates a successful completion for the Delete method. My confusion stemmed from expecting a response from the delete method when there was none.

Delete Items - Business Central API

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文