You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell调用SendGrid API添加队友时遇无效JSON错误求助

解决PowerShell调用SendGrid API添加队友时的无效JSON错误

问题根源

你的PowerShell代码生成的JSON不符合SendGrid API的格式要求,核心问题有两个:

  • is_admin被设置为字符串"false",但API要求的是布尔类型的false
  • scopes未用正确的数组语法声明,导致JSON结构不符合接口规范

修正后的PowerShell代码

$uri = "https://api.sendgrid.com/v3/teammates"
$emails = "test1@domain.com", "test2@domain.com"
$subusers = "subuser1", "subuser2"
$sendgridApiKey= "full_admin_key"

foreach ($email in $emails){
    foreach ($subuser in $subusers){
        $headers = @{
            "Authorization" = "Bearer $sendgridApiKey"
            "Content-Type" = "application/json"
            "on-behalf-of" = $subuser
        }
        $Body = @{
            email    = $email
            scopes   = @("alerts.create", "alerts.read")  # 用@()明确声明数组类型
            is_admin = $false  # 使用布尔值而非字符串
        } | ConvertTo-Json

        Write-Host "Inviting $email to subuser $subuser"
        Invoke-RestMethod -Uri $Uri -Method Post -Headers $headers -Body $body
    }
}

关键修改说明

  1. is_admin字段:将字符串"false"替换为PowerShell布尔值$false,转换后JSON会生成"is_admin": false的正确格式,匹配API对该字段的类型要求。
  2. scopes字段:用@()包裹权限值,明确声明为数组,转换后会生成"scopes": ["alerts.create", "alerts.read"]的数组结构,符合API对scopes参数的格式要求。
  3. (可选优化)用PowerShell变量插值"Bearer $sendgridApiKey"替代字符串拼接,语法更简洁规范。

额外提示

原curl代码中的JSON存在语法错误(缺少"scopes": [开头),推测你实际调用时使用了正确的格式,注意确保PowerShell代码中scopes的权限内容和你需求一致。

内容的提问来源于stack exchange,提问作者Jako

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 17:23:10