B4J Question Firebase REST API - send message need help with auth

Alexander Stolte

Expert
Licensed User
Longtime User
According to this:
"Starting from March 2020, FCM stopped creating legacy server keys. Existing legacy server keys will continue to work, but we recommend that you instead use the newer version of key labeled Server key in the Firebase console. "
They have updated the REST API to send messages to a device.

This is my code:
B4X:
'To send to a single, specific device, pass the device's registration token as shown.
'See the client setup information for your platform to learn more about registration tokens.
'https://firebase.google.com/docs/cloud-messaging/send-message#send-messages-to-specific-devices
Public Sub SendMessagesToSpecificDevices(token As String,body As String,title As String)
    
    Dim url As String = $"https://fcm.googleapis.com/v1/projects/${PROJECT_NAME}/messages:send"$
    
    Dim json As JSONGenerator
    json.Initialize(CreateMap("message":CreateMap("token":token,"notification":CreateMap("body":body,"title":title))))
    
    Dim j As HttpJob : j.Initialize("",Me)
    j.PostString(url,json.ToString)
    j.GetRequest.SetContentType("application/json")
    j.GetRequest.SetHeader("Authorization", "Bearer " & API_KEY)
    
    Wait For (j) JobDone(j As HttpJob)

End Sub
But this error comes:
B4X:
ResponseError. Reason: , Response: {
  "error": {
    "code": 401,
    "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED"
  }
}
what am I doing wrong?
 

DonManfred

Expert
Licensed User
Longtime User
Expected OAuth 2 access token
So i guess you need to use OAuth2 class to start the OAuth-Flow and to receive the AccessToken.

 
Upvote 0
Top