web-push-php: 401 Unauthorized: authorization header must be specified

  • I have read the README entirely
  • I have verified in the issues that my problem hasn’t already been resolved

Setup

  • Operating System: Linux
  • PHP Version: 7.4
  • web-push-php Version: <6.0.0>

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

  • gmp
  • mbstring
  • curl
  • openssl

Please select any browsers that you are experiencing problems with:

  • CLI

Features Used

  • VAPID Support
  • Sending with Payload

Problem

As of v6.0.0, I get a 401 Unauthorized when sending a push notification:

Client error: POST https://fcm.googleapis.com/fcm/send/.... resulted in a 401 Unauthorized response: authorization header must be specified.

I’m using minishlink/web-push-bundle and everything worked perfectly, but upgrading to v6 gives this problem. For Windows clients I get a curl timeout, for FCM I get this 401.

Any idea what causes this problem?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 16 (4 by maintainers)

Most upvoted comments

I am getting the same error. (I am not sure if my observation is related to this issue in general or has just the same error)

I did some debugging and noticed that the VAPID header is not calculated when there is no content encoding. Adding a content encoding helped:

https://github.com/web-push-libs/web-push-php/blob/bbbd3e12369d6d08a0ff5c4ff040a354bf95068c/src/WebPush.php#L262

I am not sure if the content encoding is always required, even if no payload is sent, but the example in the README doesn’t do it.

This is the code I was using for my tests:

$notifications = [
    [
        'subscription' => Subscription::create([
            'endpoint' => 'https://fcm.googleapis.com/fcm/send/dL....',
//            'contentEncoding' => 'aesgcm',
        ]),
        'payload' => null,
    ],
];

$auth = [
    'VAPID' => [
        'subject' => 'mailto:foo@bar.de',
        'publicKey' => 'BFF3IX....',
        'privateKey' => 'Mrl3....',
    ],
];
$webPush = new WebPush($auth);

(For me it looks like google requires the VAPID to accept the notification, otherwise the mentioned error is returned)

UPDATE: For Firefox it works without the contentEncoding and without a VAPID header, but Google seems to require it.

UPDATE2: Now that I used the whole payload that I get when I encode the subscription to JSON (including the keys) array it works again (at first I used only the URL like in the example). With the keys property the contentEncoding is then set automatically.

That worked for me in Chrome. I just needed to add the VAPID to the constructor as you mentioned and the encoding to the subscription .

Here is my code:

$auth = [
    'VAPID' => [
        'subject' => 'mailto:foo@bar.de',
        'publicKey' => '<My public key>',
        'privateKey' => '<My private key>',
    ],
];
$webPush = new WebPush($auth);
$subscription = json_decode('<My subscription json>', true);
$report = $webPush->sendOneNotification(
    Subscription::create($subscription),
    '{"msg":"Hello World!"}' // optional (defaults null)
);

Thanks @veldenb for pointing out. I’ve created a PR in the bundle repository.

I think it might be a bug in the bundle: https://github.com/Minishlink/web-push-bundle/issues/19#issue-728201518 In my case the VAPID-keys where not added to the library.

I have the same problem with v6.0.1

application.js

navigator.serviceWorker.getRegistration().then((registration) => {
  registration.pushManager
    .subscribe({
      userVisibleOnly: true,
        applicationServerKey: urlBase64ToUint8Array(
          WebPushVars.vapidPublcKey
        ),
      })
      .then((subscription) =>
        uploadSubscription(subscription)
      );
  });

push.php

$web_push = new WebPush();

$send_to_subscriptions = [ // onyl an example to show the structure
    [ 
        "endpoint" => "https://example.com/other/endpoint/of/another/vendor/abcdef...",
        "keys" => [
            'p256dh' => '(stringOf88Chars)',
            'auth' => '(stringOf24Chars)'
        ],
    ]
]

foreach ( $send_to_subscriptions as $subscription ) {
	$subscription                    = (array) $subscription;
	$subscription['contentEncoding'] = 'aesgcm';
	$web_push->queueNotification(
		Subscription::create( $subscription ),
		json_encode( $data )
	);
}

$success = [];
$failed  = [];
foreach ( $web_push->flush() as $report ) {
	$endpoint = $report->getRequest()->getUri()->__toString();
	if ( $report->isSuccess() ) {
		$success[] = [
			'id' => self::id_from_endpoint( $endpoint ),
		];
	} else {
		$failed[] = [
			'id'     => self::id_from_endpoint( $endpoint ),
			'reason' => $report->getReason(),
		];
	}
}