php API OneSignal With CURL i get this Error(请包含一个区分大小写的授权头:基本或不记名令牌="”具有有效的REST API密钥

6jjcrrmo  于 2022-11-21  发布在  PHP
关注(0)|答案(2)|浏览(141)

我想发送一个通知到(段)目标像[“活动用户”]与参数“included_segments”,但当我调用API使用CURL在PHP我得到这个错误:(请包含区分大小写的Authorization标头:基本或不记名令牌="“(具有有效的REST API密钥)。
然而,当我将目标从参数“included_segments”更改为“included_player_ids”时,代码运行良好。。但我想要“included_segments”。。请帮助我!
LINK to REST API Reference

  • 这是我的代码:`
function Copts($titlEN,$titlAR,$contEN,$contAR,$icon,$img){

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://onesignal.com/api/v1/notifications',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>'{
        "app_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
        "included_segments": ["Active Users"],
        "contents": {"en": "'.$contEN.'"},
        "headings": {"en": "'.$titlEN.'"},
        "global_image": "'.$img.'",
        "large_icon": "'.$icon.'"
      }',
    CURLOPT_HTTPHEADER => array(
      'Content-Type: application/json; charset=utf-8',
      'Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    )
));
 
return json_decode(curl_exec($curl),true);

}

`

vjhs03f7

vjhs03f71#

如果您将值显示给以下对象,可能会有所帮助:($titlEN,$titlAR,$contEN,$contAR,$icon,$img)
将此添加到您的CURLOPT_HTTPHEADER

'Accept: application/json'

我看了文档中的curl示例
这是他们的例子:

curl --request POST \
     --url https://onesignal.com/api/v1/notifications \
     --header 'Authorization: Basic YOUR_REST_API_KEY' \
     --header 'accept: application/json' \
     --header 'content-type: application/json' \
     --data '
{
     "app_id": "string",
     "included_segments": [
          "string"
     ],
     "external_id": "string",
     "contents": {
          "en": "English or Any Language Message",
          "es": "Spanish Message"
     },
     "name": "INTERNAL_CAMPAIGN_NAME",
     "send_after": "string",
     "delayed_option": "string",
     "delivery_time_of_day": "string",
     "throttle_rate_per_minute": 0
}'

下面是使用https://curlconverter.com/php/将curl转换为PHP的过程:
我认为所有的\n和多余的空格都可以删除。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://onesignal.com/api/v1/notifications');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization' => 'Basic YOUR_REST_API_KEY',
    'accept' => 'application/json',
    'content-type' => 'application/json',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, '\n{\n     "app_id": "string",\n     "included_segments": [\n          "string"\n     ],\n     "external_id": "string",\n     "contents": {\n          "en": "English or Any Language Message",\n          "es": "Spanish Message"\n     },\n     "name": "INTERNAL_CAMPAIGN_NAME",\n     "send_after": "string",\n     "delayed_option": "string",\n     "delivery_time_of_day": "string",\n     "throttle_rate_per_minute": 0\n}');

我不建议使用

curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Authorization' => 'Basic YOUR_REST_API_KEY',
        'accept' => 'application/json',
        'content-type' => 'application/json',
    ]);

使用此选项制作HTTPHEADER:

$headers = array();
$headers[] = 'Authorization: Basic YOUR_REST_API_KEY';
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json';

curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);

我更喜欢单独的curl_setopt()
而不是curl_setopt_array()
但我老了,快70岁了
我的个人资料照片是最新的。

ddhy6vgd

ddhy6vgd2#

问题是REST API KEYS不正确。USER AUTH KEY(而不是REST API KEYS)在以下位置找到了它:
Jmeter 板-〉设置-〉密钥和ID
请参阅此处的文档:https://documentation.onesignal.com/docs/accounts-and-keys

相关问题