我想自动化向 sendgrid 添加新队友的过程。我曾经通过 CURL 手动添加它们,效果很好。为了完全自动化,我需要通过 Powershell 实现相同的目标。我将curl 转换为invoke-restmethod,它抛出无效的json 错误。花了一天时间后,在我看来,正文与 CURL 请求完全相同,但仍然失败。谁能帮我这个?下面是我使用的代码:
$uri = "https://api.sendgrid.com/v3/teammates"
$emails = "[email protected]", "[email protected]"
$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
echo "Inviting $email to subuser $subuser"
#echo Invoke-RestMethod -Uri $Uri -Method Post -Headers $headers -Body $body
Invoke-RestMethod -Uri $Uri -Method Post -Headers $headers -Body $body
}
}
原始卷曲:
declare -a emails= ([email protected])
declare -a subusers= (realsubuser1 realsubuser2)
set -a
sendgridApiKey="RealOneInScript"
set +a
for e in ${emails[@]}
do
for s in ${subusers[@]}
do
echo "Inviting $e to subuser $s"
#invite user, scope is applicable to marketing users
curl -X "POST" "https://api.sendgrid.com/v3/teammates" -H "on-behalf-of:$s" -H "Authorization: Bearer $sendgridApiKey" -H "Content-Type: application/json" -d \
'{
"email":"'$e'","validations.email.read","validations.email.create","email_testing.read", "2fa_required"],
"is_admin":false
}'
sleep 10
done
done
它可能是 is_admin 值 - 它应该是布尔类型而不是字符串 - 尝试
$Body = @{
email= "$email"
scopes="alerts.create","alerts.read"
is_admin=$false} | ConvertTo-Json