首页 > 解决方案 > Firebase Auth REST API:使用 Twitter 进行 OAuth 登录会出现错误 32“无法对您进行身份验证”

问题描述

我正在 Node.js 中编写一个函数,通过 REST API 使用 Twitter 凭据将用户登录到 Firebase(使用请求库发出请求)。我可以使用 Twitter 凭据发布推文,但尝试登录 Firebase/accounts:signInWithIdp会返回以下错误:

{ error: 
   { code: 400,
     message: 'INVALID_IDP_RESPONSE : Failed to fetch resource from https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true, http status: 401, http response: {"errors":[{"code":32,"message":"Could not authenticate you."}]}',
     errors: [ [Object] ] } }

这是我的代码:

loginWithOAuth = (idToken, postBody, onCompletion, onError) => {
    var form = {
        postBody: querystring.stringify(postBody),
        requestUri: 'request uri',
        returnIdpCredential: 'false',
        returnSecureToken: 'true',
    }
    request.post({
        url: 'https://identitytoolkit.googleapis.com/v1/accounts:signInWithIdp?key=' + firebase_api_key,
        body: form,
        json: true
    }, (error, r, body) => {
        // ...
    });
}

postBody形式在哪里

{
    access_token: 'token',
    oauth_token_secret: 'token secret',
    providerId: 'twitter.com'
}

我的 Twitter 应用程序有权访问用户电子邮件。我还在requestUriFirebase 和 Twitter 中加入了白名单。重新生成我的应用程序和用户密钥并没有什么不同。

我错过了什么?

标签: node.jstwitterfirebase-authenticationtwitter-oauthnode-request

解决方案


postBody is not a stringified object, it is URL encoded:

var form = {
    postBody: 'access_token=[TWITTER_ACCESS_TOKEN]&oauth_token_secret=[TWITTER_TOKEN_SECRET]&providerId=twitter.com',
    requestUri: 'request uri',
    returnIdpCredential: 'false',
    returnSecureToken: 'true',
}

推荐阅读