question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error 400 UnauthorizedRegistration

See original GitHub issue

I use this code:

import java.nio.charset.StandardCharsets;
import java.security.Security;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import com.google.gson.JsonObject;

import nl.martijndwars.webpush.Notification;
import nl.martijndwars.webpush.PushService;

public class Teste {
	
	public static void main(String[] args) {
		try {
			Security.addProvider(new BouncyCastleProvider());

	        // Send notification!
			sendPushMessage(getPayload());
	        
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private static final int TTL = 255;

	public static void sendPushMessage(byte[] payload) {
		// Figure out if we should use GCM for this notification somehow
		try{
			Notification notification;
			PushService pushService;

			// Create a notification with the endpoint, userPublicKey from the subscription and a custom payload
			notification = new Notification(
					"https://fcm.googleapis.com/fcm/send/d8KX2q4goDM:APA91bH5Boq0076mY4-YdxIOrsD_pzfx6DorrD6FRaksk5sf64A3Z9cySX2JhxwOlql1wq-Bdo0SZvSmBbARZaxTgn4_O9MHbbG_JFY-ZJp0i6WauLwllglA54lBp6NkWB0q6axNHIa3",
					"BPNcSFiObeUbcCg4m5c1AybHv7NSdBE_X5YJ6ZFQfpXWnXQbDnEILz3qPe4Zb-9M9B6Lc_W20uSzVmH1ZyNuWwk=",
					"nJiZotPSQE4P4z75Igq57Q==",
					"Hello, world!"
					);

			// Instantiate the push service, no need to use an API key for Push API
			pushService = new PushService();

			// Send the notification
			HttpResponse httpResponse = pushService.send(notification);

			System.out.println(httpResponse.getStatusLine().getStatusCode());
			System.out.println(IOUtils.toString(httpResponse.getEntity().getContent(), StandardCharsets.UTF_8));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static byte[] getPayload() {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("title", "Hello");
        jsonObject.addProperty("message", "World");

        return jsonObject.toString().getBytes();
    }
}

And receive this answer:

400
<HTML>
<HEAD>
<TITLE>UnauthorizedRegistration</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>UnauthorizedRegistration</H1>
<H2>Error 400</H2>
</BODY>
</HTML>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
martijndwarscommented, Mar 1, 2019

I’m glad you got this to work @dsteen338! I’d be happy to discuss ideas that could improve the wiki. PRs that improve the documentation are very welcome.

It seems that most comments on this issue are about problems with using the library and not with the library itself, so I will close the issue now. If someone bumps into a similar issue or wants to discuss improvements to the documentation, please create a new issue.

1reaction
dsteen338commented, Feb 26, 2019

I am running into a similar issue, and am wondering if I can get some help. I got my push notifications to work on Firefox, but not on chrome. I was getting the 400 Unauthorized error like the users above.

I was originally not using VAPID, but wondered if I needed to be. Following the advice of @MartijnDwars, I added my VAPID private and public key-pair to the backend (based on this comment):

If you are using VAPID you should configure the public- and private key. The spring-boot-web-push repository contains an example on how to invoke the web push library. Specifically, line 27 of SendController.java configures the PushService with a public key and private key (generated from web-push-codelab.glitch.me).

However I am now getting a 403 forbidden instead. Firefox still working as expected. I’m not using GCM, just trying to do it myself. Is there something I’m missing/ doing wrong here? Currently using version 5.0.1.

Scratch that! I got it to work, I just needed to un-register and re-register the service worker after adding the applicationServerKey to the subscription.

My comment now becomes, that I needed to do a lot of googling and investigating to get to this point and get it working. I needed to find this thread specifically and use the example from https://github.com/MartijnDwars/spring-boot-web-push instead of the one from the Usage example in the wiki: https://github.com/web-push-libs/webpush-java/wiki/Usage-Example , I’m wondering if it might be of use to people who end up in a similar situation as me to update the wiki accordingly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix a 400 Bad Request Error (Causes and Fixes) - Kinsta
The HTTP error 400 can occur due to incorrectly typed URL, malformed syntax, or a URL that contains illegal characters.
Read more >
How to Fix a 400 Bad Request Error: 8 Easy Methods
The 400 bad request error is an HTTP status code that describes an error caused by an invalid request. Thus, the server can't...
Read more >
How to Fix the 400 Bad Request Error - Lifewire
The 400 Bad Request error means that the request you sent to the website server to view the page was somehow incorrect.
Read more >
How to Fix a 400 Bad Request Error [Causes and Fixes]
The 400 bad request is an HTTP response code sent from a web server that didn't understand the request sent from your browser....
Read more >
Status Code 400 (Bad Request) Explained & How to fix it - Ryte
A status code 400 or a code 4xx indicates a client error. When the client sends an invalid request to the server, the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found