admin/scripts/debug-upsert-small-bag.ps1
zzh 3046f26837 fix(track): forward Authorization Bearer to UpsertSmallBag
Track gateway requires the logged-in user JWT in addition to appKey/timestamp/sign headers. Capture token before async enqueue so background sync still works.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-29 12:53:28 +08:00

88 lines
3.0 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 单独调试 POST /OrderV2/UpsertSmallBag(与 SmallBagTrackOrderClient 一致:鉴权走 Header + Bearer)
param(
[string]$BaseUrl = "https://api.nanafa.com/track",
[string]$AppKey = "W2606001",
[string]$AppSecret = "ziK8UzlQ39m3kdL346XEBg8nljY8os52",
[string]$Authorization = "",
[string]$OrderNo = "",
[string]$Client = "TEST001"
)
function Get-SmallBagSign {
param([string]$Key, [long]$Ts, [string]$Secret)
$signString = "appKey=$Key&timestamp=$Ts&appSecret=$Secret"
$md5 = [System.Security.Cryptography.MD5]::Create()
$bytes = [System.Text.Encoding]::UTF8.GetBytes($signString)
$hash = $md5.ComputeHash($bytes)
-join ($hash | ForEach-Object { $_.ToString("X2") })
}
if ([string]::IsNullOrWhiteSpace($OrderNo)) {
$OrderNo = "LP-DEBUG-" + (Get-Date -Format "yyyyMMddHHmmss")
}
$timestamp = [DateTimeOffset]::UtcNow.ToUnixTimeSeconds()
$sign = Get-SmallBagSign -Key $AppKey -Ts $timestamp -Secret $AppSecret
$body = @{
client = $Client
orderNo = $OrderNo
trackNo = ""
otherNo = "DEBUG-CLIENT-001"
channel = "test01"
product = "test01"
apiCode = ""
consignee = @{
countryCode = "US"
province = "California"
postcode = "90001"
city = "Los Angeles"
email = "debug@example.com"
}
} | ConvertTo-Json -Depth 5 -Compress
$url = ($BaseUrl.TrimEnd('/')) + "/OrderV2/UpsertSmallBag"
Write-Host "========== UpsertSmallBag Debug ==========" -ForegroundColor Cyan
Write-Host "URL: $url"
Write-Host "Headers: appKey=$AppKey, timestamp=$timestamp, sign=$sign"
if (-not [string]::IsNullOrWhiteSpace($Authorization)) {
Write-Host "Headers: Authorization=<provided>"
}
Write-Host "Body:"
Write-Host ($body | ConvertFrom-Json | ConvertTo-Json -Depth 5)
Write-Host "========================================" -ForegroundColor Cyan
try {
$headers = @{
appKey = $AppKey
timestamp = "$timestamp"
sign = $sign
}
if (-not [string]::IsNullOrWhiteSpace($Authorization)) {
$headers.Authorization = $Authorization
}
$response = Invoke-WebRequest -Uri $url -Method POST `
-Headers $headers `
-ContentType "application/json; charset=utf-8" `
-Body ([System.Text.Encoding]::UTF8.GetBytes($body)) `
-UseBasicParsing
Write-Host "HTTP $($response.StatusCode)" -ForegroundColor Green
Write-Host $response.Content
}
catch {
if ($_.Exception.Response) {
$reader = New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream())
$errBody = $reader.ReadToEnd()
Write-Host "HTTP $([int]$_.Exception.Response.StatusCode)" -ForegroundColor Red
if ($errBody) { Write-Host $errBody } else { Write-Host "(empty body)" }
}
else {
Write-Host $_.Exception.Message -ForegroundColor Red
}
Write-Host ""
Write-Host "Tip: track gateway requires Authorization Bearer from logged-in user, plus appKey/timestamp/sign headers." -ForegroundColor Yellow
exit 1
}