Firebase REST auth when creating token with node.js admin sdk

假如想象 提交于 2019-12-03 00:18:26

The token you are using to authenticate to the Firebase REST API is not the correct type of token. You are using a Firebase Auth custom token, which can only be used to authenticate one of the Firebase client SDKs via the signInWithCustomToken() method as explained in Sign in using custom tokens on clients.

In order to authenticate to the Firebase REST API, you have two options: Firebase ID tokens (for user-based access) or Google OAuth2 access tokens (for admin access).

Authenticate with Firebase ID Tokens

See Retrieve ID tokens on the client for an explanation of how to retrieve access tokens in the various Firebase client SDKs. You can also exchange a Firebase custom token for an ID token and refresh token pair via an undocumented REST API:

Endpoint: https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=<API_KEY>

Method: POST

Request Body: { "token": <CUSTOM_TOKEN>, "returnSecureToken": true }

<API_KEY> is the same API key you get from your Firebase Console that you use in the Firebase clients. <CUSTOM_TOKEN> is a Firebase custom token.

Since ID tokens expire after an hour, you will need to use the refresh token to refresh them via this other undocumented REST API:

Endpoint: https://securetoken.googleapis.com/v1/token?key=<API_KEY>

Method: POST

Request Body: { "refresh_token": <REFRESH_TOKEN>, "grant_type": "refresh_token" }

<API_KEY> is the same API key as before. <REFRESH_TOKEN> is the refresh token from the previous API call.

Once you have an ID token, you can pass that to the REST API via the auth query parameter to authenticate a request. The request respects Firebase Security Rules as if the end user logged into the client was making the request.

Authenticate with Google Access Tokens

To authenticate with a Google OAuth2 access token, the first thing you need to do is get one. See Retrieving an access token for an explanation of how to do this. It only currently includes a Java example, but this is possible in many languages, including Node.js. Once you have an ID token, you can pass that to the REST API via the access_token query parameter to authenticate a request. The request will be made with admin access, overriding all Firebase Security Rules and granting full read and write access.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!