webpush-java: Invalid VAPID token: A value in the vapid claims is either missing or incorrectly specified
When using this library I am getting Invalid VAPID token. This occurs randomly sometimes you won’t get it at all. Sometimes you do.
[main] DEBUG *.*.*.PushService - header Authorization WebPush <jwt>
16:43:57.748 [main] DEBUG *.*.*.PushService - header Content-Encoding aesgcm
16:43:57.748 [main] DEBUG *.*.*.PushService - header Encryption salt=<salt>
16:43:57.748 [main] DEBUG *.*.*.PushService - header TTL 2419200
16:43:57.748 [main] DEBUG *.*.*.PushService - header Crypto-Key dh=BHGQ****dw=;p256ecdsa=BB_****
16:43:57.748 [main] DEBUG *.*.*.PushService - header Content-Type application/octet-stream
The response I am getting is
{"origin":"remote","type":"response","correlation":"b7b0107273db5d1e","duration":2227,"protocol":"HTTP/1.1","status":401,"headers":{"Connection":["keep-alive"],"Content-Length":["296"],"Content-Type":["application/json"],"Date":["Sat, 02 Jul 2022 14:44:00 GMT"],"Server":["nginx"]},"body":{"code":401,"errno":109,"error":"Unauthorized","message":"Invalid VAPID token: A value in the vapid claims is either missing or incorrectly specified (e.g. \"exp\":\"12345\" or \"sub\":null). Please correct and retry.","more_info":"http://autopush.readthedocs.io/en/latest/http.html#error-codes"}}
Subscription subscription = new Subscription();
subscription.endpoint = endpoint;
subscription.keys = new Keys();
subscription.keys.auth = auth;
subscription.keys.p256dh = p256dh;
ObjectMapper mapper = new ObjectMapper();
var webPushMessage = new WebPushMessage();
webPushMessage.setBody("A new document was uploaded");
webPushMessage.setTitle("Document uploaded");
var notification = new Notification(subscription, mapper.writeValueAsString(webPushMessage));
var logbook = createLogbook();
var client = httpClient(logbook);
var pushService = new PushService(keys.getPublicKey(), keys.getPrivateKey(), client);
pushService.send(notification);
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 1
- Comments: 15
You just have to add the Subject in your push service, nothing else changes…
Here is what I changed and solved the problem (but it was a long time ago, I’m not entirely sure…) push = new PushService(); push.setPrivateKey(vapiPrivateKey); push.setPublicKey(vapiPublicKey); push.setSubject(“mailto:xxx”); //— Needed for firefox
What was odd is that in the specification the “Subject” is not mandatory, but Firefox (not chrome) makes it mandatory, so they are violating the specifications.
Good luck
@sbodmer for me it turned out that the problem was not in the library at all. From experience I found out that Firefox requires
subparameter in the notification. When I started providing email assubparameter “Invalid VAPID token” problem never happened to me with the latest version of the webpush-java.Bingo, I added the “sub” part and now it works, thanks a lot my friend ;^) I read the spec and the “sub” is not mandatory…
Just to add to this, I found another Firefox issue. The
expmust be an int. Both Google and Apple handle float just fine but for Firefox it can not have a decimal or it returns the same 401@aistomin Yes that’s correct
@aistomin Yes that’s what I did. I think you only need to changed three or 4 lines and exception. Something along these lines