Cloud Functionsでデプロイした関数をHTTPトリガーで呼び出す

p0dee
Jan 23, 2021

--

Firebase初心者が、Webサーバーが直接Firebaseと連携することなく、FCMを用いてPush通知を発火する方法を模索していて見つけた手法のメモ。

Firebase Cloud Functionsでデプロイした任意の関数は、[さまざまなトリガーを用いて](https://cloud.google.com/functions/docs/calling?hl=ja)呼び出すことができる。そのうちのひとつにHTTPトリガーと言うものがある。

[HTTP トリガー](https://cloud.google.com/functions/docs/calling/http?hl=ja)

基本的に上記に従えば問題ないが、事前にgcloudコマンドラインツールを使えるようにする必要があった。→ [クイックスタート: gcloud コマンドライン ツールの使用](https://cloud.google.com/functions/docs/quickstart?hl=ja)

HTTP関数の実装について以下記事では、サンプルソースコード付きで詳しく解説してあるので、これに従って簡単なGETやPOSTリクエストを試行することができた。

[HTTPのチュートリアル](https://cloud.google.com/functions/docs/tutorials/http?hl=ja)

一方で、本来やりたかったFCMの利用には及んでおらず。

var admin = require('firebase-admin');
var app = admin.initializeApp();

に対して以下のエラーログが表示されたり

Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. Initializing firebase-admin will fail

Push通知送信処理に対するエラーが以下となっているので、Google Cloudのプロジェクトと、Firebaseプロジェクトとの疎通がうまくいっていないのか、なんなのか、分かっていないので調査中。

Error: An error occurred when trying to authenticate to the FCM servers. Make sure the credential used to authenticate this SDK has the proper permissions. See https://firebase.google.com/docs/admin/setup for setup instructions. Raw server response: " PROJECT_NOT_PERMITTED Error 401 ". 
Status code: 401.

Firebaseプロジェクト上のCloud FunctionsではPush通知送信できているので、以下記事によると、`firebase deploy`でもHTTPトリガーを用いて実行可能な関数をデプロイできそうなので、試してみる。

[Get started: write, test, and deploy your first functions](https://firebase.google.com/docs/functions/get-started)

--

--

No responses yet