gpt4 book ai didi

android - 将 firebase 身份验证与谷歌应用引擎云端点集成

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:31 25 4
gpt4 key购买 nike

有人可以指定(使用一些示例代码)如何验证谷歌云端点中的 firebase token 吗?最近提出的问题根本没有澄清它(How to integrate firebase authentication with google app engine endpoints)

端点中的 Google 身份验证是通过将用户参数添加到端点来自动完成的。可以使用 facebook graph api 在云端点中验证 Facebook token ,如下所示:

    @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 token 的验证和facebook token 一样容易吗?是否可以从 Firebase token 中检索电子邮件?

最佳答案

据我了解文档,您似乎需要将用户 token 添加到您的请求中,例如作为 header 。然后您需要针对 Firebase admin sdk 验证此 token ,这样您将获得用户 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

How do I secure my Google Cloud Endpoints APIs with Firebase token verification?

关于android - 将 firebase 身份验证与谷歌应用引擎云端点集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39124587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com