I am encountering a 403 Forbidden
error when attempting to send a notification using the OneSignal::sendNotificationToUser
function in Laravel. The error message suggests that the request is being denied due to a missing or invalid API key in the Authorization header. Below is the error message from the exception:
Client error: `POST https://onesignal.com/api/v1/notifications` resulted in a `403 Forbidden` response:
{"errors": ["Access denied. Please include an 'Authorization: Basic ...' header with a valid API key"]}
Here’s the code I’m using to send the notification:
try {
OneSignal::sendNotificationToUser(
$message,
$player_id,
$url = null,
$data = null,
$buttons = null,
$schedule = null
);
} catch (\Exception $e) {
return response()->json([
"error" => 'one_signal_error',
"message" => $e->getMessage(),
"file" => $e->getFile(),
"line" => $e->getLine(),
], 422);
}
{
"error": "one_signal_error",
"message": "Client error: `POST https://onesignal.com/api/v1/notifications` resulted in a `403 Forbidden` response:\n{\"errors\": [\"Access denied. Please include an 'Authorization: Basic ...' header with a valid API key (https:\/\/documenta (truncated...)\n",
"file": "/home/arloponc/public_html/developer/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php",
"line": 113
}
The notification should be sent successfully when the correct OneSignal API key is used.
The request results in a 403 Forbidden
response, indicating that the API key is either missing or invalid. I have already verified the API key and added it to the environment variables, but the issue persists.
sendNotificationToUser
function to send a notification.403 Forbidden
error in the response..env
file, and I'm confident that it's correctly configured.Any guidance or suggestions would be appreciated.
Please include valid API key with your request header.
It seems like you're using the GitHub package and may be the configuration portion is not properly done.
After everything run php artisan optimize:clear
to clear the configuration cache. Cross verify your secrets with your actual credentials.