尝试使用 Laravel 中的
403 Forbidden
函数发送通知时遇到 OneSignal::sendNotificationToUser
错误。该错误消息表明,由于授权标头中的 API 密钥丢失或无效,请求被拒绝。以下是异常的错误消息:
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"]}
这是我用来发送通知的代码:
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
}
使用正确的 OneSignal API 密钥时,通知应成功发送。
请求会产生
403 Forbidden
响应,表明 API 密钥丢失或无效。我已经验证了 API 密钥并将其添加到环境变量中,但问题仍然存在。
sendNotificationToUser
功能发送通知。403 Forbidden
错误。.env
文件中设置,我确信它配置正确。如有任何指导或建议,我们将不胜感激。
请在您的请求标头中包含有效的 API 密钥。