如何从FirebaseAuth获取ID令牌 [英] How to get the ID token from FirebaseAuth

查看:121
本文介绍了如何从FirebaseAuth获取ID令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从我的Firebase数据库中读取一些Json文件,并尝试获取要在标头中使用的当前用户的ID令牌,如下所示:

I am reading some Json files from my firebase database and I am trying to obtain an ID token for the current user to be used in the header as follows:

 var response = await httpClient.get(url,headers: {'Authorization':"Bearer ${FirebaseAuth.instance.currentUser.getToken()}"});

当我执行上一行时,它似乎没有获得正确的令牌,因为我不能访问数据库,但是,当我在字符串中手动包含ID令牌时,我的应用程序将按预期工作。我做错了到底是什么?

when I do the previous line, it seems that it does not get the correct token as I can not access the database, however, when I manually include an ID token in the string, my application works as intended. What is it exactly that I am doing wrong ?

推荐答案

getIdToken() 返回 Future -您必须 await 它实际上得到令牌字符串:

getIdToken() returns a Future - you have to await it to actually get the token string:

var token = await FirebaseAuth.instance.currentUser().getIdToken();
var response = await httpClient.get(url,headers: {'Authorization':"Bearer $token"});

这篇关于如何从FirebaseAuth获取ID令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆