有人可以指定(使用一些示例代码)如何验证谷歌云端点中的 firebase 令牌吗?最近提出的问题根本没有澄清(如何将 Firebase 身份验证与 Google 应用引擎端点集成 https://stackoverflow.com/questions/37330997/how-to-integrate-firebase-authentication-with-google-app-engine-endpoints)
端点中的 Google 身份验证是通过将用户参数添加到端点来自动完成的。
Facebook 令牌可以在云端点中使用 facebook graph api 进行验证,如下所示:
@ApiMethod(name = "endpoint.addUser", httpMethod = HttpMethod.POST)
public ResultObject addUser(HttpServletRequest request, User pUser) throws OAuthRequestException {
String token = request.getHeader("Authorization");
String graphUrl = "https://graph.facebook.com/v2.6/me?fields=id,name,email&access_token=" + token;
URL u = new URL(g);
URLConnection c = u.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
String inputLine;
StringBuffer b = new StringBuffer();
while ((inputLine = in.readLine()) != null){
b.append(inputLine + "\n");
}
in.close();
graph = b.toString();
JSONObject json = new JSONObject(graph);
facebookId = json.getString("id");
email = json.getString("email");
//...
}
firebase 令牌的验证是否像 facebook 令牌一样简单?是否可以从 firebase 令牌检索电子邮件?
据我了解文档,您似乎需要将用户令牌添加到您的请求中,例如作为标头。然后您需要根据 Firebase admin sdk 验证此令牌,这样您就可以获得用户 ID。
@ApiMethod(name = "someApiCall", httpMethod = ApiMethod.HttpMethod.POST)
public YourResponse someApiCall(YourRequestObject body, HttpServletRequest httpRequest) {
String userToken = httpRequest.getHeader("USER_TOKEN_HEADER");
Task<FirebaseToken> authTask = FirebaseAuth.getInstance().verifyIdToken(userToken)
.addOnSuccessListener(new OnSuccessListener<FirebaseToken>() {
@Override
public void onSuccess(FirebaseToken firebaseToken) {
}
});
try {
Tasks.await(authTask);
} catch (ExecutionException e) {
} catch (InterruptedException e) {
}
FirebaseToken result = authTask.getResult();
String userId = result.getUid();
return new YourResponse();
}
我的代码基于:
https://firebase.google.com/docs/auth/admin/verify-id-tokens https://firebase.google.com/docs/auth/admin/verify-id-tokens
如何通过 Firebase 令牌验证来保护我的 Google Cloud Endpoints API? https://stackoverflow.com/questions/38418472/how-do-i-secure-my-google-cloud-endpoints-apis-with-firebase-token-verification
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)