简体   繁体   English

具有ID令牌的REST API Firestore身份验证

[英]REST API Firestore authentication with ID Token

I have a Firestore DB and one user. 我有一个Firestore DB和一个用户。 I am able to retrieve an ID Token for the user after they sign-in via a request with REST. 我可以通过REST请求登录后为用户检索ID令牌。

I am to use the ID Token with requests to read/write from the DB. 我将ID令牌用于从DB读取/写入的请求。

I am able to read and write to the Firestore when the security rules are open (anyone can read/write) in this manner, with Python requests: 使用Python请求,当安全规则打开(任何人都可以读/写)时,我能够读取和写入Firestore:

headers = {
    'Content-Type': 'application/json',
}

url = 'https://firestore.googleapis.com/v1beta1/projects/[PROJECT_NAME]/databases/(default)/documents/[COLLECTION]/[DOCUMENT]'

r = requests.get(url, headers=headers)

Writing is similar, and omitted. 写作类似,省略。

Attempting to append the ID Token as indicated by these docs for Realtime Database gives the error message ""Invalid JSON payload received. Unknown name \\"auth\\""... 尝试附加实时数据库这些文档所指示的ID令牌会给出错误消息""Invalid JSON payload received. Unknown name \\"auth\\""... ""Invalid JSON payload received. Unknown name \\"auth\\""...

My question is how to authorise a user ID Token for Firestore via REST? 我的问题是如何通过REST授权Firestore的用户ID令牌?

According to the Cloud Firestore REST API guide , 根据Cloud Firestore REST API指南

After you obtain either a Firebase ID token or a Google Identity OAuth 2.0 token, pass it to the Cloud Firestore endpoints as an Authorization header set to Bearer {YOUR_TOKEN}. 获取Firebase ID令牌或Google Identity OAuth 2.0令牌后,将其作为授权标头设置为Bearer {YOUR_TOKEN}传递到Cloud Firestore端点。

So your request should include the header 所以你的请求应该包括标题

'Authorization': `Bearer ${YOUR_TOKEN}`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM