web-push-libs / web-push-php

Web Push library for PHP
MIT License
1.69k stars 295 forks source link

Sending a notification results in a `401 Unauthorized` response. (`authorization header must be specified`) #325

Closed Tikolu closed 3 years ago

Tikolu commented 3 years ago

Please confirm the following:

Setup

Please provide the following details, the more info you can provide the better.

Please check that you have installed and enabled these PHP extensions :

Please select any browsers that you are experiencing problems with:

Please specify the versions (i.e. Chrome Beta, Firefox Beta etc).

Problem

Sending a notification to a FCM endpoint does not work. The endpoint sends back a 401 response with the following message: authorization header must be specified

Expected

The notification would be sent to the endpoint without any errors. According to the usage section of the README, this library supports fcm.googleapis.com endpoints for Chrome.

Features Used

Example / Reproduce Case


use Minishlink\WebPush\WebPush;
use Minishlink\WebPush\Subscription;

$publicKey = "(88 character key)";

$webPush = new WebPush([
    "subject" => "(link to website)",
    "publicKey" => $publicKey,
    "privateKey" => "(44 character key)"
]);

$subscription = Subscription::create([
    "endpoint" => "https://fcm.googleapis.com/fcm/send/...",
    "publicKey" => $publicKey,
    "authToken" => "(24 character key)",
   "contentEncoding" => "aes128gcm"
]);

$response = $webPush -> sendOneNotification($subscription);
print_r($response); // -> Client error: `POST (endpoint)` resulted in a `401 Unauthorized` response: authorization header must be specified.

I would greatly appreciate if someone could help me out with this issue, as I've been struggling with it for a few days now. Thank you very much.

Tikolu commented 3 years ago

Okay, I managed to find what I was doing wrong. When initialising$webPush, I was entering all security keys directly, instead of placing them all under a "VAPID" key.

I was entering my security keys like this:

$webPush = new WebPush([
    "subject" => "(link to website)",
    "publicKey" => $publicKey,
    "privateKey" => "(44 character key)"
]);

But I should have entered them like this:

$webPush = new WebPush([
    "VAPID" => [
        "subject" => "(link to website)",
        "publicKey" => $publicKey,
        "privateKey" => "(44 character key)"
    ]
]);