webpush-java icon indicating copy to clipboard operation
webpush-java copied to clipboard

400 UnauthorizedRegistration

Open Tsyklop opened this issue 6 years ago • 0 comments

use https://web-push-codelab.glitch.me/ for getting keys and your sendPushMessage method from UsageExample

I get 400 UnauthorizedRegistration error.

Java code:

if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
      Security.addProvider(new BouncyCastleProvider());
}

SubscriptionMy subscriptionMy = new SubscriptionMy(p256dh, auth, endpoint);
sendPushMessage(subscriptionMy, getPayload().getBytes(Charset.forName("UTF-8")));
public void sendPushMessage(SubscriptionMy sub, byte[] payload) throws GeneralSecurityException, InterruptedException, JoseException, ExecutionException, IOException {

        // useGcm = true OR false -> same error
        boolean useGcm = true;
        Notification notification;
        PushService pushService;

        if (useGcm) {
            // Create a notification with the endpoint, userPublicKey from the subscription and a custom payload
            notification = new Notification(
                    sub.getEndpoint(),
                    sub.getKey(),
                    sub.getAuth(),
                    payload
            );

            // Instantiate the push service, no need to use an API key for Push API
            pushService = new PushService();
        } else {
            // Or create a GcmNotification, in case of Google Cloud Messaging
            notification = new Notification(
                    sub.getEndpoint(),
                    sub.getKey(),
                    sub.getAuth(),
                    payload
            );

            // Instantiate the push service with a GCM API key
            pushService = new PushService(GCM_API_KEY);
        }

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

        System.out.println(response);
    }
public class SubscriptionMy {

    private String key;
    private String auth;
    private String endpoint;

    public SubscriptionMy(String key, String auth, String endpoint) {
        this.key = key;
        this.auth = auth;
        this.endpoint = endpoint;
    }

    public String getEndpoint() {
        return endpoint;
    }

    public void setAuth(String auth) {
        this.auth = auth;
    }

    public String getAuth() {
        return auth;
    }

    /**
     * Returns the base64 encoded auth string as a byte[]
     */
    public byte[] getAuthAsBytes() {
        return Base64.getDecoder().decode(getAuth());
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }

    /**
     * Returns the base64 encoded public key string as a byte[]
     */
    public byte[] getKeyAsBytes() {
        return Base64.getDecoder().decode(getKey()); 
    }

    /**
     * Returns the base64 encoded public key as a PublicKey object
     */
    public PublicKey getUserPublicKey() throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
        KeyFactory kf = KeyFactory.getInstance("ECDH", BouncyCastleProvider.PROVIDER_NAME);
        ECNamedCurveParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1");
        ECPoint point = ecSpec.getCurve().decodePoint(getKeyAsBytes());
        ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, ecSpec);

        return kf.generatePublic(pubSpec);
    }

}

Im not use getKeyAsBytesmethod because i get Exceptionjava.lang.IllegalArgumentException: Illegal base64 character 5f`

And Im not use getUserPublicKeymethod because i get Exceptionjava.lang.IllegalArgumentException: Invalid point encoding 0x42`

Please. help me.

Tsyklop avatar Jun 12 '18 17:06 Tsyklop