-
Notifications
You must be signed in to change notification settings - Fork 295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
401 Unauthorized: authorization header must be specified #293
Comments
Hi, can you try on v6.0.1 please? |
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(),
];
}
} |
In my understanding, if I create the push subscription with the publicKey, the subscription itself should be enough to authenticate against the push Service, right? |
Even if I add VAPID settings to the Constructor $web_push = new WebPush( [
'VAPID' => self::get_vapid(),
] ); or to queueNotification $web_push->queueNotification(
Subscription::create( $subscription ),
json_encode( $data ),
[],
[
'VAPID' => self::get_vapid(),
]
); it does not help. Still 401 for Chrome and Firefox Subscriptions. |
Nope, still doesn't work :( |
I just found out that this only happens on my local environment (MAMP). If I upload the exact same code to my remote host it works. @stephanvierkant, could this be the same on your system? |
Nope, it doesn't work either. But my CI, local system and production system are quite similar in my case. Maybe you could try to find what makes a difference between your local system and remote host? |
@stephanvierkant, those are completely different systems.. remote it's a shared hosting where I don't have a lot of possibilities. But in that case I'm sure it depends on some system settings.. |
@nico-martin Could the difference between your environments be about GMP and/or BCMath PHP extensions ? |
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: Line 262 in bbbd3e1
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:
(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 |
Strange. With encoding it works for me in Chrome, but not in Firefox:
|
I think it might be a bug in the bundle: Minishlink/web-push-bundle#19 (comment) |
Thanks @veldenb for pointing out. I've created a PR in the bundle repository. |
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)
); |
Setup
Features Used
Problem
As of v6.0.0, I get a 401 Unauthorized when sending a push notification:
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?
The text was updated successfully, but these errors were encountered: