尝试围绕 Firebase REST API 编写一个包装器(请参阅https://github.com/cloudfuji/taika以获取完整源代码),并且身份验证令牌似乎失败了。这些函数是 Firebase 提供的 Java 库选项 ( https://github.com/firebase/firebase-token-generator-java )的简单包装器
代码很简单:
(ns taika.auth
(:require [clojure.string :as string]
[clj-http.client :as client]
[cheshire.core :as json])
(:import [com.firebase.firebase-token-generator.security.token]
[org.json.JSONOBject]))
(defn token-generator [secret-key]
(com.firebase.security.token.TokenGenerator. secret-key))
(defn create-token [token-generator auth-data & [admin?]]
(let [token-options (doto (com.firebase.security.token.TokenOptions.)
(.setAdmin (or admin? false)))]
(.createToken token-generator (org.json.JSONObject. auth-data) token-options)))
生成令牌,看起来很合理(例如密钥,当然):
(let [tg (token-generator "abc123")
auth-data {:email "example@example.com" :api_key "my-api-key"}
admin? false]
(create-token tg auth-data admin?))
=> "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ2IjowLCJpYXQiOjEzNjIxNjEzMDJ9.8701406fad76b2dff83bf38a18d91a95ed7787f255e7dd534a77e7daa0c58c59"
但是在 REST API 请求中使用令牌时,它会失败并显示:
{ "error" : "invalid_token: Could not parse auth token." }
ruby 库似乎没有同样的问题。
同样,完整的源代码位于https://github.com/cloudfuji/taika/blob/master/src/taika/auth.clj