Hot questions for Using Vapor in firebase

Question:

Swift + Vapor framework for server + Xcode 8.1

I am trying to read Firebase Realtime Database making HTTP requests to my DB, but I get permission denied.

These are the steps: 1. create JWT sign it with secret key downloaded from "console.developers.google.com" 2. send POST request to OAuth2 server and get access token 3. send GET request to firebase database with access token received from OAuth2 server.

I get "Permission denied", HTTP/1.1 403 Forbidden

// the header of the JSON Web Token (first part of the JWT)
let headerJWT = ["alg":"RS256","typ":"JWT"]

// the claim set of the JSON Web Token
let jwtClaimSet =
  ["iss":"firebase-adminsdk-kxx5h@fir-30c9e.iam.gserviceaccount.com",
 "scope":"https://www.googleapis.com/auth/firebase.database", //is this the correct API to access firebase database?
 "aud":"https://www.googleapis.com/oauth2/v4/token",
 "exp": expDate,
 "iat": iatDate]


drop.get("access") { request in
var accesstoken = "ya29.ElqhA-....XXXX"

 let responseFirebase = try drop.client.get("https://fir- 30c9e.firebaseio.com/data/Users.json",
  headers: ["Authorization":"Bearer \(accesstoken)"], 
     query: [:])

print("FirebaseResponse_is \(responseFirebase)")
return "success"
}


Answer:

TLDR; Try placing auth=<TOKEN> in your query string instead of using the authorization header.


The Firebase documentation is unclear on how this works. According to the documentation, there are three methods that should work.

  1. auth=<TOKEN> in query string (link)
  2. access_token=<TOKEN> in query string (link)
  3. Authorization: Bearer <TOKEN> in request header (link)

I'm not convinced that all three methods do actually work however. I'm using method 1 in my application, so I know that one works for sure.

Question:

I am trying to make a POST request to a Firebase Notifications API using Vapor 1.5 and Firebase Legacy Protocol, but I get failure response.

response is JSON(node: Node.Node.object(["multicast_id": Node.Node.number(5936281277445399934), "failure": Node.Node.number(0), "canonical_ids": Node.Node.number(0), "results": Node.Node.array([Node.Node.object(["message_id": Node.Node.string("0:1527074314969790%c7ade8b9f9fd7ecd")])]), "success": Node.Node.number(1)]))

EDIT Making the request through POSTMan fails with error "The request was missing an Authentication Key (FCM Token)."

class FirebaseRequester {
 let fcmLegacyServerKey = "AIzaSyDSuXXXXXXkCafTQay5_r8j3snvVos"

 func sendNotification(payLoad: JSON) throws -> Response {

    var response: Response?
    do {
        let responseFCM = try drop.client.post("https://fcm.googleapis.com/fcm/send", 
           headers: ["Content-Type":"application/json","Authorization": "key\(fcmLegacyServerKey)"], 
           query: [:], 
          body: payLoad.makeBody())

        response = responseFCM

    }catch let error {
        let message = error.localizedDescription
        logErr.prints(message: message)
        throw Abort.custom(status: .badRequest, message: message)
    }

    guard let rsp = response?.json else {


        let message = "no json received on line \(#line)"
        drop.log.error(message)
        logErr.prints(message: message)
        throw Abort.custom(status: .badRequest, message: message)
     }
  print("rsp in json format is \(rsp)")
      return response!
 }//end of sendNotification()
}//end of class FirebaseRequester




      //make another class here and initialize it with  FirebaseRequester
      //get data from Client App 
      // validate data 
      // finally, create the payLoad and call sendNotification(:)
     //request should look like 
{
  "aps": {
    "alert": "Breaking News!",
    "sound": "default",
    "link_url": "https://raywenderlich.com"
 }
}

     let fcmKeyToSendTo = "someDeviceTokenKeyReceivedFromClient_biHZNI-e9E53WEkCzrki"

            let data = try Node(node: ["alert": "alert", "sound": "sound", "link_url": "https://www.someWebsite.com"])

     var payLoadObj = try JSON(node: ["aps" : data])
     payLoadObj["to"] = try JSON(node: fcmKeyToSendTo)

            do {
                let _ = try firebaseRequester.sendNotification(payLoad: payLoadObj)
            }catch{
                logErr.prints(message: error.localizedDescription)
            }

            let message = "notification Sent"
            return try JSON(node:["success":message])


Answer:

  1. In sendNotification(payload:) I had a typo, I missed = after key. It should have been "key=\(fcmLegacyServerKey)"
  2. In sendNotification(payload:), payLoad.makeBody should not be called, I should have just passed the JSON object payLoad as an argument to the .post request.
  3. The JSON object of the notification was clearly badly formatted from the outset. The message type I wanted to send was notification, but I was passing in a key named aps. I should have passed key notification as shown below.

.

class FirebaseRequester {

  let fcmLegacyServerKey = "AIzaSy....vVos"

  func sendNotification(payLoad: JSON) throws -> Response {

  var response: Response?
   do {
     let responseFCM = try drop.client.post("https://fcm.googleapis.com/fcm/send", 
       headers: ["Content-Type":"application/json","Authorization": "key=\(fcmLegacyServerKey)"], 
       query: [:], 
      body: payLoad

      response = responseFCM

  }catch let error {
     let message = error.localizedDescription
     logErr.prints(message: message)
     throw Abort.custom(status: .badRequest, message: message)
 }
   guard let rsp = response?.json else {

    let message = "no json received on line \(#line)"
    drop.log.error(message)
    logErr.prints(message: message)
    throw Abort.custom(status: .badRequest, message: message)
   }
    return response!
  }//end of sendNotification()
}//end of class FirebaseRequester



class TestRouteNow {

  let firebaseRequester: FirebaseRequester

  init(firebaseRequester: FirebaseRequester) {
     self.firebaseRequester = firebaseRequester
  }

  func addRoutes(drop: Droplet) {
     drop.post("test", "notif", handler: postNotification)
  }

   func postNotification(request: Request) throws -> ResponseRepresentable {

   let fcmDevice = "someDeviceTokenReceivedFromClientApp"
   let data = try Node(node: ["title": "title","body": "body", "sound": "default", "badge":"60"])

    var payLoadObj = try JSON(node: ["notification": data])
    payLoadObj["to"] = try JSON(node: fcmDevice)

      do {
        let _ = try firebaseRequester.sendNotification(payLoad: payLoadObj)
            }catch{
                logErr.prints(message: error.localizedDescription)
            }

            let message = "notification Sent"
            return try JSON(node:["success":message]) 
     } 
 }//end of class




    // request body
{
  "to" : "cQDtm_someDeviceTokenReceivedFromClient",
  "priority":"high",

 "notification": {
     "title":"Booking Rescheduled",
      "body": "Cancelled Booking 7830593, for Mon, 12 March",
      "sound":"default",
     "badge": "100"
  }
}

Question:

I am writing to Firebase Database from a Vapor server using the Firebase REST API. Why don't I get a Status code in the header received? This would enable me to check if the write was successful or not.

headerReceived items line 83 in extensionPutRequestToUsersClaimBookingCleaner are

([Connection: "keep-alive", Cache-Control: "no-cache", Server: "nginx", Date: "Sun, 27 Aug 2017 21:47:25 GMT", Content-Type: "application/json; charset=utf-8", Content-Length: "1027", Strict-Transport-Security: "max-age=31556926; includeSubDomains; preload", Access-Control-Allow-Origin: "*"])


Answer:

The response's status code is returned in the status property rather than the headers property.

guard response.status == .created else {
  // not a 201 response
}

Question:

I followed some tutorials with Vapor and despite that I understood how to Get or Put or Patch some JSON values on the server, I have no idea how to send dictionary to Firebase from within the app.

I've tried to write some data on Firebase using Postman and I have no problem, but I have no idea how to handle the parameters of the request using Vapor ?

Can anyone, please, tell me where I have to put the URL of the database and how to make the request? Or even better, if there's some example on GitHub, I can look at?


Answer:

You need to create JWT token in order to get access to Firebase Database. That's the hardest part.

let drop = Droplet()
func readUsersNodeInFb()  {

    let customer = userStripeIdCustomer
    do {
        let responseFirebase = try drop.client.get(
            "https://bev-708.firebaseio.com/Users/\(usersUID!)/\(customer!)/\(bookingNumber!).json", headers: ["Authorization":"Bearer \(TokenData.acessTokenReceived!)"], query: [:],
            body: " a JSON Object")


        responseFirebaseAssigned = responseFirebase
    } catch let error {

        let errorString = "error line 115 in ClaimBookingCleaner received when trying to update  Firebase, \(error.localizedDescription)"
        errorFirebaseReceived = errorString
        return
    }


    //response from Firebase
    let bodyReceived = responseFirebaseAssigned?.body.bytes

    do {
        let jsonFire = try JSON(bytes:bodyReceived!)
         JsonFirebase = jsonFire
         print("jsonFire is \(jsonFire)")
    } catch let error {

        let errorString = "jsonFire could not be built with the payload received from server, line 55 in ClaimBookingCleaner error \(String(describing: error.localizedDescription))"
        errorFirebaseReceived = errorString
        return
    }