我正嘗試從GAE中的Channel API遷移到Firebase。要做到這一點,首先,我試圖建立一個本地開發環境。我從GAE樣本中克隆了示例應用程序。 (Link to sample)從頻道api遷移到firebase
當我運行這個時,當Web客戶端嘗試使用Firebase DB進行身份驗證時,出現以下錯誤。錯誤在控制檯中。
即令牌認證failed.Clearly,這點到生成JWT是不正確的事實。
可以肯定的,我也做了以下內容:
- 創建於谷歌的雲主機服務帳戶。
- 下載的JSON,並指出這JSON在環境變量「GOOGLE_APPLICATION_CREDENTIALS」
- 將/從火力剪斷成WEB-INF /視圖的代碼firebase_config.jspf文件
生成令牌的代碼是作爲(來自FirebaseChannel.java)如下
public String createFirebaseToken(Game game, String userId) { final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService(); final BaseEncoding base64 = BaseEncoding.base64(); String header = base64.encode("{\"typ\":\"JWT\",\"alg\":\"RS256\"}".getBytes()); // Construct the claim String channelKey = game.getChannelKey(userId); String clientEmail = appIdentity.getServiceAccountName(); System.out.println(clientEmail); long epochTime = System.currentTimeMillis()/1000; long expire = epochTime + 60 * 60; // an hour from now Map<String, Object> claims = new HashMap<String, Object>(); claims.put("iss", clientEmail); claims.put("sub", clientEmail); claims.put("aud", IDENTITY_ENDPOINT); claims.put("uid", channelKey); claims.put("iat", epochTime); claims.put("exp", expire); System.out.println(claims); String payload = base64.encode(new Gson().toJson(claims).getBytes()); String toSign = String.format("%s.%s", header, payload); AppIdentityService.SigningResult result = appIdentity.signForApp(toSign.getBytes()); return String.format("%s.%s", toSign, base64.encode(result.getSignature())); }
代替步驟#2,也已經嘗試「gcloud AUTH應用缺省登錄」,然後取消設置的環境變量之後運行 - 導致SAM e問題
感謝此方面的任何幫助。