Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use postman for onCall methods made as functions in firebase?

So I've made this very simple method, where I want to sign in with to firebase using a custom token. As of right now it's simply

export const createCustomToken = functions.region('europe-west1').https.onCall(async (data, context) => {

    try {
        const firebaseJWT = await authenticationProvider.createCustomToken()
        console.log(firebaseJWT.toString())
    } catch (err) {
        throw new functions.https.HttpsError('internal', 'Something went wrong ');
    }
});

The authenticationProvider looks like this:

return firebase.admin.auth().createCustomToken("Some uid")
       .then((token) => {
           console.log("Did create custom token.");
           return token;
       }).catch((error) => {
           console.log("Error creating custom token:" + error);
           throw new firebase.functions.https.HttpsError('internal', 'createCustomToken(uid) has failed for some reason');
       }) 
}

I tried using the shell method to run it locally, doing this line in command prompt: firebase functions:shell and then calling authenticationController.createCustomToken({})

which just gives me this response: Error creating custom token:Error: Failed to determine service account. Make sure to initialize the SDK with a service account credential. Alternatively specify a service account with iam.serviceAccounts.signBlob permission. Original error: Error: Error while making request: getaddrinfo ENOTFOUND metadata metadata:80. Error code: ENOTFOUND

The service account seems to have the proper roles, so I don't think that's the issue.

So I was wondering if there was any way I could call me createCustomToken() function from Postman for instance, and see the response so I can test my code properly, without having an app implement the onCall method to see the results?

Or what am I missing to run this through the firebase functions:shell command?

like image 651
Mikkel Larsen Avatar asked Feb 22 '19 08:02

Mikkel Larsen


1 Answers

You can test firebase functions on Postman using request headers as described in official documentation here

First you'll have to serve your functions locally using following command at your project's root folder

firebase serve

You can also check official documentation how to serve functions locally here

On running above command you'll get an http link for your function. Something like this

http://localhost:5000/{YOUR_FIREBASE_PROJECT_NAME}/us-central1/myFunction

Use that link for POST request in Postman. In the "Body" section of Postman select "Raw" then "JSON (application/json)" from dropdown and type following sample of body in the body section

{
    "data": {}
}

Adding this json object with "data" argument in the body is required as mentioned in the documentation here. Check following screenshot for reference

Screenshot displaying sample body for requesting onCall method in Postman

Now you can send request to get response from your function. Hope it helps :)

Note: There is a mistyped "j" in the body of request in screenshot. Please ignore that.

like image 153
MAZ Avatar answered Oct 17 '22 08:10

MAZ