ken_nogi/lineworks-sync/test-board-posts.ps1
Kenichiro NOGI 88a402ce0f up
2026-07-10 18:13:30 +09:00

95 lines
3.0 KiB
PowerShell

# ============================================================
# LINE WORKS Board API - Get Posts Test
#
# Usage:
# .\test-board-posts.ps1 -BoardId 4080000000780956700
# ============================================================
param(
[Parameter(Mandatory = $true)]
[string]$BoardId,
[string]$PostId,
[int]$Count = 40
)
# Load .env into process environment variables (simple parser)
if (Test-Path ".env") {
Get-Content ".env" -Encoding UTF8 | ForEach-Object {
if ($_ -match '^\s*([^#][^=]*)=(.*)$') {
$name = $matches[1].Trim()
$value = $matches[2].Trim()
if ($name -and $value) {
[System.Environment]::SetEnvironmentVariable($name, $value, "Process")
}
}
}
Write-Host "[INFO] .env loaded" -ForegroundColor Cyan
}
else {
Write-Host "[WARN] .env not found in current directory" -ForegroundColor Yellow
}
# --- 1. Get access token (delegates JWT signing to Node.js helper) ---
# stdout = token only, stderr = debug info (redirected to a temp file)
Write-Host "[INFO] Requesting access token..." -ForegroundColor Cyan
$debugLogPath = Join-Path $env:TEMP "lw_token_debug.log"
$accessToken = & node get-token.js 2>$debugLogPath
if (Test-Path $debugLogPath) {
Write-Host "`n--- Debug info from get-token.js ---" -ForegroundColor DarkGray
Get-Content $debugLogPath | ForEach-Object { Write-Host $_ -ForegroundColor DarkGray }
Write-Host "-------------------------------------`n" -ForegroundColor DarkGray
}
if ([string]::IsNullOrWhiteSpace($accessToken)) {
Write-Host "[ERROR] Failed to obtain access token (see debug info above)" -ForegroundColor Red
exit 1
}
$shortToken = $accessToken.Substring(0, [Math]::Min(20, $accessToken.Length))
Write-Host "[OK] Access token obtained (first 20 chars: $shortToken...)" -ForegroundColor Green
# --- 2. Call the API directly (single post if -PostId given, otherwise list) ---
if ($PostId) {
$uri = "https://www.worksapis.com/v1.0/boards/$BoardId/posts/$PostId"
}
else {
$uri = "https://www.worksapis.com/v1.0/boards/$BoardId/posts?count=$Count"
}
Write-Host "`n[INFO] Request URL: $uri" -ForegroundColor Cyan
$headers = @{
"Authorization" = "Bearer $accessToken"
}
try {
$response = Invoke-WebRequest -Uri $uri -Headers $headers -Method Get -ErrorAction Stop
Write-Host "`n[SUCCESS] Status code: $($response.StatusCode)" -ForegroundColor Green
Write-Host $response.Content
}
catch {
$statusCode = $null
$body = $null
if ($_.Exception.Response) {
$statusCode = [int]$_.Exception.Response.StatusCode
try {
$stream = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($stream)
$body = $reader.ReadToEnd()
}
catch {
$body = "(failed to read response body)"
}
}
Write-Host "`n[FAILED] Status code: $statusCode" -ForegroundColor Red
Write-Host "Response body:" -ForegroundColor Red
Write-Host $body
}