“error”:“invalid_token:无法解析身份验证令牌。”在Clojure中为Firebase生成JWT时

时间:2013-03-01 18:10:05

标签: clojure firebase jwt

尝试编写围绕Firebase REST API编写包装器(请参阅https://github.com/cloudfuji/taika获取完整源代码),并且auth令牌似乎失败了。这些函数是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

1 个答案:

答案 0 :(得分:1)

此错误是由Java令牌生成器库中的错误引起的。现在已经修好了。拉下更改并再次拍摄。

https://github.com/firebase/firebase-token-generator-java

相关问题