gpt4 book ai didi

android - 使用 Google Play 服务在 Appengine 上进行 Oauth

转载 作者:太空宇宙 更新时间:2023-11-03 12:38:11 25 4
gpt4 key购买 nike

我在 Android 上使用 Google Play 服务来访问 Google Apis 和 Google 云端点。我还可以使用来自 Google Play 服务的 token 访问 appengine User api。这可能吗? link 中有一些示例代码对于 OAuth,但有点模糊。我可以在 header 中传递 oauth token 并使用以下代码获取用户吗??

    User user = null;
try {
OAuthService oauth = OAuthServiceFactory.getOAuthService();
user = oauth.getCurrentUser();

} catch (OAuthRequestException e) {
// The consumer made an invalid OAuth request, used an access token that was
// revoked, or did not provide OAuth information.
// ...
}

最佳答案

可以,但是这种方法不会像您所处的场景那样安全:

  • 为 Google API 使用特定于服务的范围
  • 直接访问 Endpoints API

您可以使用 Google Play 服务来 obtain a token对于您要使用的范围。由于您对在 App Engine 上使用 Users API 感兴趣,因此您需要 userinfo.email 范围:

String mScope = "https://www.googleapis.com/auth/userinfo.email";
try {
token = GoogleAuthUtil.getToken(mActivity, mEmail, mScope);
} catch {
...
}

通过授权 header 将其发送到 App Engine:

Authorization: Bearer your_token

然后,使用OAuth API,您可以获得一个User对象:

String mScope = "https://www.googleapis.com/auth/userinfo.email";
User user = null;
try {
OAuthService oauth = OAuthServiceFactory.getOAuthService();
user = oauth.getCurrentUser(mScope);
} catch (OAuthRequestException e) {
// The consumer made an invalid OAuth request, used an access token that was
// revoked, or did not provide OAuth information.
// ...
}

但是,通常您不想这样做!如果您以这种方式保护您的应用程序,则另一个用户可以编写一个应用程序来请求您对您的 userinfo.email范围。一旦获得授权,他们所需要做的就是获取 token 并将其传递给您的应用程序,他们就会像您一样出现。端点和其他 Google API 有额外的逻辑来防止这种行为,这就是为什么您最好使用其中一种方法。

关于android - 使用 Google Play 服务在 Appengine 上进行 Oauth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13501317/

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