【问题标题】:CURL script to powershellCURL 脚本到 powershell
【发布时间】:2019-07-06 15:07:55
【问题描述】:

我正在尝试使用 Invoke-RestMethod 将 curl 转换为 powershell 以进行单信号推送

用于一个信号的脚本:

curl --include \
     --request POST \
     --header "Content-Type: application/json; charset=utf-8" \
     --header "Authorization: Basic YOUR_REST_API_KEY" \
     --data-binary "{\"app_id\": \"YOUR_APP_ID\",
\"contents\": {\"en\": \"English Message\"},
\"included_segments\": [\"Subscribed Users\"]}" \
     https://onesignal.com/api/v1/notifications

我已尝试使用以下示例,但没有成功。

   $uri = "https://onesignal.com/api/v1/notifications"
      $parameters = @{
        app_id = 'YOUR_APP_ID'
        contents = "en: English Message"
        included_segments = 'Subscribed Users'
        data = 'foo:bar'
      }
      $parameters | Invoke-RestMethod -Uri $uri -Method Post

我已经使用这个 powershell 脚本进行 pushover,效果很好,但现在我想转移到 onesignal,但我在使用 app_id 将消息转发给用户的情况下,在哪里/如何将 rest api 密钥放入其中时遇到了问题。

代码截取自:https://documentation.onesignal.com/v5.0/reference#section-example-code-create-notification

我希望有人可以帮助我解决这个问题。

问候

【问题讨论】:

    标签: json powershell curl


    【解决方案1】:

    我不知道这个端点是如何工作的,但它应该像这样工作:

    $key = "Basic RESTAPIKEY"
    $headers = @{}
    $headers.Add("Authorization",$Key)
    $headers.Add("Content-Type","application/json; charset=utf-8")
    
    $uri = "https://onesignal.com/api/v1/notifications"
    $parameters = @{
        app_id = 'YOUR_APP_ID'
        contents = "en: English Message"
        included_segments = 'Subscribed Users'
        data = 'foo:bar'
    } | ConvertTo-Json
    
    Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body ([System.Text.Encoding]::UTF8.GetBytes($parameters)) -ContentType "application/json"
    

    如果数据二进制部分是使用 PowerShell 发送的,您还可以使用 Invoke-RestMethod 中的 -InFile 参数。

    如果只是阅读您发布的链接并认为正文部分应该如下所示:

    $parameters = @{
        app_id = "5eb5a37e-b458-11e3-ac11-000c2940e62c"
        included_segments = "Array of active users"
        data = @{
            foo = "bar"
        }
        contents = @{
            en = "English Message"
        }
    } | ConvertTo-JSON
    

    【讨论】:

    • 嗯,应该和这个差不多吧? $headers = "授权:基本 xxxxxxxx REST API KEY xxxxxxxx" $uri = "onesignal.com/api/v1/notifications" $parameters = @{ app_id = "xxxxxxxxx ONESIGNAL APP ID xxxxxxxxxx" included_segments = "All" data = @{ foo = "bar" }内容 = @{ en = "英文留言" } } | ConvertTo-JSON Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $parameters -ContentType "application/json" 但这也不起作用,我从 onesignal 获得 2 个密钥,一个用于 rest api,一个用于 app id .
    • 这是它在 python 中的完成方式: import requests import json header = {"Content-Type": "application/json; charset=utf-8", "Authorization": "Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj"} payload = {"app_id": "5eb5a37e-b458-11e3-ac11-000c2940e62c", "included_segments": ["Active Users"], "contents": {"en": "English Message"}} req = requests.post ("onesignal.com/api/v1/notifications", headers=header, data=json.dumps(payload)) print(req.status_code, req.reason)
    • 我编辑了一些位,也许你可以用编辑后的键、正文和标题再试一次..
    • Invoke-RestMethod :必须使用适当的属性或方法修改“Content-Type”标头。参数名称:name At line:1 char:1 + Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body([System.Text.E ... + ~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Invoke-RestMethod], ArgumentException + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand跨度>
    【解决方案2】:

    试试下面的

    $basicAuth = "Basic REST_API_KEY";
    $headers = @{ Authorization = $basicAuth };
    $uri = "https://onesignal.com/api/v1/notifications";
    $body = @{ app_id = 'YOUR_APP_ID'; contents = @{ en = 'English Message' }; included_segments = @('Subscribed Users'); data = @{ foo = 'bar' }} | ConvertTo-Json;
    
    Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -ContentType "application/json; charset=utf-8" -Body ([System.Text.Encoding]::UTF8.GetBytes($body));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-30
      • 2018-03-10
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      相关资源
      最近更新 更多