Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get 403 response with the "new" Firebase Cloud Messaging API

We are successfully using the Legacy HTTP Server Protocol on our server for FCM. I wanted to update to FCM HTTP v1 API today.

I did it step by step and when the server calls the request, we get this response:

Server returned HTTP response code: 403 for URL: https://fcm.googleapis.com/v1/projects/[projectid]/messages:send

This is the server code:

URL url = new URL("https://fcm.googleapis.com/v1/projects/[projectid]/messages:send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "Bearer " + getAccessToken());
conn.setRequestProperty("Content-Type", "application/json");
OutputStream outputStream = conn.getOutputStream();
outputStream.write(req.getBytes("UTF-8"));

// Exception happen here
InputStream inputStream = conn.getInputStream();

The getAccessToken():

private static String getAccessToken() throws IOException {
        GoogleCredential googleCredential = GoogleCredential
            .fromStream(new FileInputStream(ClientApiServlet.context.getRealPath("/WEB-INF/[projectid].json")))         .createScoped(Arrays.asList("https://www.googleapis.com/auth/firebase.messaging"));
        googleCredential.refreshToken();
        return googleCredential.getAccessToken();
}

I have downloaded the json file from the adminsdk page of the firebase cloud.

All with the same projectid...

I updated these 2 libs on the server:

google-http-client-jackson2-1.23.0.jar
google-oauth-client-1.23.0.jar

The getAccessToken() methode returned an accesstoken: "ya29.c.Elr0BAa..."

I think, I miss a small step, maybe you could help? Thanks in advance!

Edit: It is working now with the hint of arterpa! Thanks again!

After that I got a 400 error, so something in the request data was wrong:

The problem was, we didn't converted all data{...}values to strings. With the legacy protocol it was not an issue, but with FCM HTTP v1 API it has to be strings! ;)

like image 353
3dmg Avatar asked Oct 30 '17 15:10

3dmg


People also ask

Is there a limit on Firebase messaging?

Maximum message rate to a single device For Android, you can send up to 240 messages/minute and 5,000 messages/hour to a single device.

What is Registration_ids in FCM?

The registration_ids parameter refers to the Registration Tokens you want to add in that specific Device Group. Described as: An ID generated by the FCM SDK for each client app instance. Required for single device and device group messaging. Note that registration tokens must be kept secret.


1 Answers

I had this problem, and it seems you need to enable FCM API for your project at Google API console.

like image 161
arterpa Avatar answered Oct 14 '22 21:10

arterpa