【问题标题】:Firebase Auth: Can not get a new token using refresh token RESTFirebase 身份验证:无法使用刷新令牌 REST 获取新令牌
【发布时间】:2020-08-05 09:49:46
【问题描述】:

我按照这个从刷新令牌中获取令牌:https://firebase.google.com/docs/reference/rest/auth/#section-refresh-token

  let response = await axios.post(`https://securetoken.googleapis.com/v1/token?key=${FIREBASE_CONFIG.apiKey}&grant_type=refresh_token&refresh_token=${refreshToken}`,
    {}, {
      data: qs,
      headers: { "content-type": "application/x-www-form-urlencoded" }
    }
  );

它过去可以工作,但现在无法正常工作。 在 Chrome 控制台中出现此错误:

从源“http://localhost:8080”访问位于“https://securetoken.googleapis.com/v1/token?key=&grant_type=refresh_token&refresh_token=”的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:否“Access-Control-Allow-”请求的资源上存在 Origin' 标头。

【问题讨论】:

  • axios 向请求中添加了哪些其他标头?通过检查 devtools 的网络窗格中的请求来检查。更改您的前端代码以删除 axios 添加的任何其他标头。或者改为使用fetch() 发出请求,这使您可以直接完全控制标头。我这么说是因为问题中的错误表明浏览器正在执行 CORS 预检 OPTIONS 请求,并且只有 content-type: application/x-www-form-urlencoded 标头的 POST 到 https://firebase.google.com/docs/reference/rest/auth/#section-refresh-token 不会触发预检

标签: firebase google-chrome google-api firebase-authentication cors


【解决方案1】:

我最近看到了你的问题。 也许我下面的建议会有所帮助

const url : string = `https://securetoken.googleapis.com/v1/token?key=${environment.apiKey}`;
    
const body : any = {
    grant_type : 'refresh_token',
    refresh_token : this.userValue.refresh_token
}
    
return this.http.post<any>(url, body, {withCredentials : false});

问候

【讨论】:

    【解决方案2】:

    sideshowbarker 是对的,axios 实例确实很少有用于其他目的的 HTTP 标头。使用干净的 axios 实例后,错误消失了。不过,从 googleapis 返回的错误可能更加不言自明

    【讨论】:

      猜你喜欢
      • 2019-02-27
      • 2017-11-08
      • 2019-12-08
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      相关资源
      最近更新 更多