从频道API迁移到Firebase



我试图从GAE中的通道API迁移到Firebase。首先,我正在尝试设置当地开发环境。我从GAE样品中克隆了示例应用程序。(链接到样品)

当我运行此功能时,我会遇到以下错误,当Web客户端试图使用Firebase DB进行身份验证时。错误在控制台中。

错误的屏幕截图

即令牌身份验证失败。显然,这表明生成的JWT是不正确的事实。

可以肯定的是,我已经完成了以下操作:

  1. 在Google Cloud Console中创建了一个服务帐户。
  2. 下载了JSON,并指向环境变量中的此JSON" Google_application_credentials"
  3. 将firebase剪切的代码放入Web-Inf/view/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应用程序默认登录",然后在解开环境变量后运行 - 导致同一问题

感谢在这方面的任何帮助。

进一步研究后,我发现了其他信息,这可能有助于其他人面临同一问题。我做了以下操作,以便能够在本地运行样本。

  1. 重要的是要确保附录中的服务帐户具有访问资源的权限。选择"编辑器"作为权限的角色(其他级别也可能起作用,但我选择了此级别)作为默认的Appengine服务帐户。这将确保您不会遇到"未经授权"错误。

  2. 我的应用程序已启用用于域身份验证,并且没有使用Google身份验证。但是,已为Google身份验证创建了样本。我必须更改示例代码以作为URL的一部分发送userID,并删除了参考用户服务的代码。

  3. 即使现在,控制台错误确实出现了,但是该应用程序工作正常。可能会忽略此错误。但是,在部署的环境中,此错误未显示。

如果Google/Firebase工程师通过官方答复更新此答案,或者适当地更新示例文档,因为目前似乎在任何地方都没有提及此信息。

最新更新