DEV Community

Evan Lin
Evan Lin

Posted on • Originally published at evanlin.com on

[Golang][GCP] Firebase Database on Cloud Functions: Tips and Pitfalls

title: [Golang][GCP] Things to note and potential pitfalls when using Golang to control Firebase Database on Cloud Function
published: false
date: 2024-04-09 00:00:00 UTC
tags: 
canonical_url: https://www.evanlin.com/til-golang-firebase-cf/
---

# Preface:

This article will quickly go through how to apply for a GCP Services Account Credential, how to use this data in Cloud Function, and finally how to use Golang to control Firebase Database in Cloud Function, as well as potential pitfalls.

## Applying for Services Account Credential to allow Cloud Function to connect to Google services

The teaching of this part can actually refer to another article of mine. [[Learning Document] How to use Golang to access Google Cloud services on Heroku](https://www.evanlin.com/til-heroku-gcp-key/), but I will quickly go through it here.

- Enter Google Cloud Console, go to IAM & Admin and select Create Services Account

![image-20240413221505536](https://www.evanlin.com/images/2022/image-20240413221505536.png)

- Services Account Name is up to you, pay attention to (the project above and Firebase **project name must be consistent**)

![image-20240413222847247](https://www.evanlin.com/images/2022/image-20240413222847247.png)

- Grant this service account access to project. When setting the identity, it is recommended to use Editor first (it's a larger role, use with caution)

![image-20240413223055288](https://www.evanlin.com/images/2022/image-20240413223055288.png)

- “Grant users access to this service account” does not need special settings
- Press “Manage Keys” to prepare to download Credential

![image-20240413223225404](https://www.evanlin.com/images/2022/image-20240413223225404.png)

- Select Add Key -> Create new Key -> Download JSON

![image-20240413223613244](https://www.evanlin.com/images/2022/image-20240413223613244.png)

## Things to note when using Golang Google Options package:

Although Firebase Realtime Database has been set to allow everyone to read and write, if you access it through Golang, you will get an Unauthorized request error message. This is because the Project in your JSON file is different from your Firebase Project. Just recreate a Services Account and update the JSON content.

![image-20240413220630196](https://www.evanlin.com/images/2022/image-20240413220630196.png)

# How to import Services Account Credential in Google Cloud Function?

Next, I will share how to correctly use it within Cloud Function. If you want to directly use Cloud Function to open the Credential JSON file, you will always get an error message that you cannot get the credential correctly.

At this time, you need to add it through environment variables:

- Copy all the content in the JSON file
- Set the `GOOGLE_APPLICATION_CREDENTIALS` parameter, and then paste all the content into the environment parameter.

![image-20240413225710980](https://www.evanlin.com/images/2022/image-20240413225710980.png)

- Next, I will tell you how to modify the relevant code?

Enter fullscreen mode Exit fullscreen mode
// Init firebase related variables
ctx := context.Background()
opt := option.WithCredentialsJSON([]byte(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")))
config := &firebase.Config{DatabaseURL: os.Getenv("FIREBASE_URL")}
app, err := firebase.NewApp(ctx, config, opt)
if err != nil {
    log.Fatalf("error initializing app: %v", err)
}
client, err := app.Database(ctx)
if err != nil {
    log.Fatalf("error initializing database: %v", err)
}
Enter fullscreen mode Exit fullscreen mode

- First, `option.WithCredentialsJSON([]byte(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")))` allows you to read the credential from the environment parameter.
- Next, `&firebase.Config{DatabaseURL: os.Getenv("FIREBASE_URL")}` sets the FIREBASE\_URL content.
- This way, it can be executed correctly. Next, we will look at the relevant processing of Gemini chat memory.

## Reference articles:

- [[Learning Document] How to use Golang to access Google Cloud services on Heroku](https://www.evanlin.com/til-heroku-gcp-key/)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)