使用邮件和密码通过 REST API [Firebase] 进行身份验证

Using mail and password to authenticate via the REST API [Firebase]

我想知道是否可能实际上向 Firebase RESTAPI 验证 使用 自定义身份验证?

我已经使用 Firebase 一段时间了,目前正在考虑将我的后端迁移到 Firebase。 使用后端的应用目前使用 REST API,根本不需要实时数据。 因此,我只想在客户端上使用 REST API 而不是完整的 Android 框架。

是否可以通过 HTTP 请求使用 Firebase 的邮件和密码身份验证获取身份验证令牌?

old docs I've only found a solution with custom login and in the new docs 中,您似乎需要一个 Google 服务帐户。

感谢任何帮助或建议。

一旦您使用电子邮件和密码进行身份验证,您就需要 return 令牌,根据文档,您可以 return 用户的令牌 getToken(opt_forceRefresh),可在以下 URL.

https://firebase.google.com/docs/reference/js/firebase.User#getToken

如果您尝试通过 REST API,那么您必须在 您的应用程序 中完成所有操作。

只需获取 json 数据 并检查你的身份验证与否

使用 retrofit Get 方法 并从您的 firebase 应用程序 中获取所有数据。

这是我的 post Rerofit + Firebase,我 post 为初学者了解 firebase 和 Retrofit 的联系。

请浏览此链接,它会对您有所帮助......

REST auth

User Authnitication

Example

享受编码......

更新:Firebase REST 身份验证现已记录在案!

View the documentation


Firebase REST 身份验证

通过检查 Javascript API.

发送的请求,我弄清楚了如何为 Firebase 执行电子邮件和密码身份验证

这些 API 未记录且不受支持


Firebase 3

Firebase 3 身份验证是 Google Identity Toolkit 的更新和重命名版本。旧文档不完全准确,但可能有用,可在此处找到:https://developers.google.com/identity/toolkit/web/reference/

Firebase 3 要求所有请求在 header

中包含 Content-Type: application/json

API键

Firebase 3 要求将 API 密钥附加到所有身份验证请求。您可以通过访问 Firebase 项目概述并单击 "Add Firebase to your web app" 找到数据库的 API 键。您应该会看到一个 window,其中包含如下代码:

<script src="https://www.gstatic.com/firebasejs/live/3.0/firebase.js">    </script>
<script>
  // Initialize Firebase
  var config = {
    apiKey: "<my-firebase-api-key>",
    authDomain: "my-firebase.firebaseapp.com",
    databaseURL: "https://my-firebase.firebaseio.com",
    storageBucket: "my-firebase.appspot.com",
  };
  firebase.initializeApp(config);
</script>

复制 apiKey 值并保存以备后用。

注册

方法:POST

URL: https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=<my-firebase-api-key>

有效载荷:

{
    email: "<email>",
    password: "<password>",
    returnSecureToken: true
}

响应:

{
    "kind": "identitytoolkit#SignupNewUserResponse",
    "localId": "<firebase-user-id>", // Use this to uniquely identify users
    "email": "<email>",
    "displayName": "",
    "idToken": "<provider-id-token>", // Use this as the auth token in database requests
    "registered": true,
    "refreshToken": "<refresh-token>",
    "expiresIn": "3600"
}

登录

方法:POST

URL: https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=<my-firebase-api-key>

有效载荷:

{
    email: "<email>",
    password: "<password>",
    returnSecureToken: true
}

响应:

{
    "kind": "identitytoolkit#VerifyPasswordResponse",
    "localId": "<firebase-user-id>", // Use this to uniquely identify users
    "email": "<email>",
    "displayName": "",
    "idToken": "<provider-id-token>", // Use this as the auth token in database requests
    "registered": true,
    "refreshToken": "<refresh-token>",
    "expiresIn": "3600"
}

获取帐户信息

方法:POST

URL: https://www.googleapis.com/identitytoolkit/v3/relyingparty/getAccountInfo?key=<my-firebase-api-key>

有效载荷:

{
    idToken: "<provider-id-token>"
}

响应:

{
    "kind": "identitytoolkit#GetAccountInfoResponse",
    "users": [
    {
        "localId": "<firebase-user-id>",
        "email": "<email>",
        "emailVerified": false,
        "providerUserInfo": [
        {
            "providerId": "<password>",
            "federatedId": "<email>",
            "email": "<email>",
            "rawId": "<email>"
        }],
        "passwordHash": "<hash>",
        "passwordUpdatedAt": 1.465327109E12,
        "validSince": "1465327108",
        "createdAt": "1465327108000"
    }]
}

Firebase 2

这些请求 return JSON Firebase 文档中描述的数据。 https://www.firebase.com/docs/web/guide/login/password.html#section-logging-in

登录

您可以通过发送格式如下的 GET 请求进行身份验证:

https://auth.firebase.com/v2/<db_name>/auth/password?&email=<email>&password=<password>

注册

也可以通过将 _method=POST 作为查询字符串的一部分发送相同的 GET 请求来创建用户

https://auth.firebase.com/v2/<db_name>/users?&email=<email>&password=<password>&_method=POST

来自 Firebase 指南使用自定义身份验证系统在网站上使用 Firebase 进行身份验证(请参阅 https://firebase.google.com/docs/auth/web/custom-auth

You can integrate Firebase Authentication with a custom authentication system by modifying your authentication server to produce custom signed tokens when a user successfully signs in. Your app receives this token and uses it to authenticate with Firebase.

这里是关键点:

1) 将 Firebase 添加到您的 Web 项目并使用 Firebase REST JavaScript SDK 进行身份验证,并使用 Firebase 访问存储/实时数据库。

  // TODO: Replace with your project's customized code snippet
  <script src="https://www.gstatic.com/firebasejs/3.0.2/firebase.js"></script>
  <script>
    // Initialize Firebase
    var config = {
      apiKey: '<your-api-key>',
      authDomain: '<your-auth-domain>',
      databaseURL: '<your-database-url>',
      storageBucket: '<your-storage-bucket>'
    };
    firebase.initializeApp(config);
  </script>

2) 您的应用程序用户使用他们的用户名和密码登录到您的身份验证服务器。您的服务器检查凭据和 returns 自定义令牌是否有效。

3) 从身份验证服务器收到自定义令牌后,将其传递给 signInWithCustomToken 以登录用户

firebase.auth().signInWithCustomToken(token).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // ...
});

我相信您可以执行以下操作之一:

GitHub 上还有用于生成 Firebase 令牌的项目: