This sample will show you how to connect your mobile app to Google Cloud Platform. To do this, you will be implementing something called a token service. The token service provides your application with short-lived OAuth2.0 tokens, which can be used to communicate with GCP’s APIs. The reason you need an OAuth2.0 token is that a GCP API requires the caller to be authenticated and authorized to access an API.
To do this, you will set up the token service which will be implemented on Cloud Functions for Firebase. The token service handles requests from the client app and retrieves from cloud firestore database or creates the OAuth2.0 token credentials via the Cloud IAM API. These tokens are then stored in a Cloud Firestore database so that they can be used for future requests from a client, until they expire. To ensure clients don’t receive an expired token, the service validates that credentials haven't expired before triggering a push notification from Firebase Cloud Messaging back to the calling client.
View the source code
Before using the sample app, make sure that you have the following prerequisites:
- Node.js 8
- Firebase Command Line Interface (CLI) Tool
- Create a project (or use an existing one) in the Google Cloud Console
To configure the sample you must declare the required environment variables.
The sample requires the following environment variables:
GOOGLE_CLOUD_PROJECT: The project id of your GCP project.GOOGLE_APPLICATION_CREDENTIALS: The path to your API key file.
Use the following commands to declare the required environment variables:
export GCF_REGION=us-central1
export GOOGLE_CLOUD_PROJECT=[your-GCP-project-id]
export BASE_URL=http://localhost:8010/$GOOGLE_CLOUD_PROJECT/$GCF_REGION
export GOOGLE_APPLICATION_CREDENTIALS=[path-to-your-API-key-file]
Run the following command to set the gcloud projectID and sign in to firebase :
functions config set projectId $GOOGLE_CLOUD_PROJECT
firebase login
Run the following command which prompts you to choose from one of your existing projects:
firebase use --add
-
Clone this repository:
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git cd nodejs-docs-samples/functions/tokenService -
Run the following command to deploy the "getOAuthToken" function:
firebase deploy --only functions -
To run the tests, use the following commands from the
functions/tokenservice folder:
```
npm install && npm test
```
In AuthLibrary FCMTokenProvider.swift has implemented how to call getOAuthToken API
Please refer ios-docs-samples
Please refer android-docs-samples