Home > Mobile >  API OneSignal With CURL i get this Error (Please include a case-sensitive header of Authorization: B
API OneSignal With CURL i get this Error (Please include a case-sensitive header of Authorization: B

Time:11-20

i want to send a notification to (Segments) target like ["Active Users"] with parameter "included_segments" but When i call the API using CURL in PHP i get this Error : (Please include a case-sensitive header of Authorization: Basic or Bearer token="" with a valid REST API key.).

however the code is running well when i change target from parameter "included_segments" to "include_player_ids" .. but i want "included_segments" .. please HELP Me!.

LINK to REST API Reference

  • this is my Code : `
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);

}

`

CodePudding user response:

It might help if you showed the values to: ($titlEN,$titlAR,$contEN,$contAR,$icon,$img)

Add this to your CURLOPT_HTTPHEADER

'Accept: application/json'

I looked at the example curl in the documentation
This is their example:

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
}'

This is how their curl translates to PHP using https://curlconverter.com/php/:
I think all the \n and extraneous spaces could be removed.

$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}');

I do not recommend using

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

Use this to make your 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);

I prefer the individual curl_setopt()
rather than curl_setopt_array()
but I'm old, almost 70 years old.
and my profile photo is current.

CodePudding user response:

The problem was the REST API KEYS are not correct. USER AUTH KEY instead of REST API KEYS found it here :

dashboard -> settings -> keys & ids

See documentation here : https://documentation.onesignal.com/docs/accounts-and-keys

.

  • Related