2017-09-02 72 views
0

我正嘗試從GAE中的Channel API遷移到Firebase。要做到這一點,首先,我試圖建立一個本地開發環境。我從GAE樣本中克隆了示例應用程序。 (Link to sample從頻道api遷移到firebase

當我運行這個時,當Web客戶端嘗試使用Firebase DB進行身份驗證時,出現以下錯誤。錯誤在控制檯中。

Screenshot of the error

即令牌認證failed.Clearly,這點到生成JWT是不正確的事實。

可以肯定的,我也做了以下內容:

  1. 創建於谷歌的雲主機服務帳戶。
  2. 下載的JSON,並指出這JSON在環境變量「GOOGLE_APPLICATION_CREDENTIALS」
  3. 將/從火力剪斷成WEB-INF /視圖的代碼firebase_config.jspf文件
  4. 生成令牌的代碼是作爲(來自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問題

感謝此方面的任何幫助。

回答

0

經過進一步研究,我發現了更多的信息,可以幫助其他人面臨同樣的問題。我做了以下操作以便能夠在本地運行示例。

  1. 確保appengine中的服務帳戶具有訪問資源的權限非常重要。選擇「編輯器」作爲權限角色(其他級別也可以,但我選擇了這種方式)默認的appengine服務帳戶。這將確保您不會遇到「未經授權」錯誤。

  2. 我的應用程序已啓用域身份驗證,並且未使用Google身份驗證。但是,該示例是爲Google身份驗證創建的。我不得不對示例代碼進行更改,以將userId作爲URL的一部分發送,並刪除了引用UserServiceFactory的代碼。

  3. 控制檯錯誤甚至現在確實顯示,但應用程序工作正常。這個錯誤可能可以忽略。但是,在部署的環境中,此錯誤不顯示。

如果谷歌/火力地堡的工程師更新這個答案與官方的反應,或更新樣本文件我將不勝感激適當作爲當前這個信息似乎並沒有在任何地方提到。