gpt4 book ai didi

android - Google+ 和 AppEngine 身份验证

转载 作者:太空狗 更新时间:2023-10-29 16:41:25 26 4
gpt4 key购买 nike

我有一个已经安装了 Google+ 的 Android 应用程序,我想知道我是否可以使用此连接对 Google AppEngine 端点进行身份验证。

Google 示例代码是:

   settings = getSharedPreferences(TAG, 0);
credential = GoogleAccountCredential.usingAudience(this, ClientCredentials.AUDIENCE);
setAccountName(settings.getString(PREF_ACCOUNT_NAME, null));
Tictactoe.Builder builder = new Tictactoe.Builder(
AndroidHttp.newCompatibleTransport(), new GsonFactory(),
credential);
service = builder.build();

这很好,但我要重新进行身份验证。我能否以任何方式利用 PlusClient 来避免创建更多凭据?

谢谢

最佳答案

您可以执行以下步骤来使用您的 google 加号来验证 Google AppEngine 端点的用户:

1、使用google plus获取账户名,这一步会用到

String accountName = mPlusClient.getAccountName();

现在,当您通过 google plus 获取帐户名称时,您不必使用示例代码中提供的帐户选择器

2, 将上述步骤中的帐户名保存到 sharedpreferences 和 credential:

private void setAccountName(String accountName) {
SharedPreferences.Editor editor = settings.edit();
editor.putString(PREF_ACCOUNT_NAME, accountName);
editor.commit();
credential.setAccountName(accountName);
this.accountName = accountName;
}

3,为要用作受众的 Web 应用程序创建客户端 ID。我想这应该在 api 控制台的同一个项目中,您已经在其中为您的 android 应用程序创建了一个客户端 ID。将此 Web 应用客户端 ID 设置为 ClientCredentials.AUDIENCE 值。

4,在您的带有云端点的应用引擎项目中,为 @endpoints.api 装饰器或 @endpoints.method 装饰器的 allowed_client_ids 参数提供 Android 客户端 ID 和 Web 客户端 ID。您还必须将 @endpoints.api 装饰器的受众参数设置为 Web 客户端 ID。

5、在你的android应用中,使用账户名和受众详细信息创建一个google凭据对象并将其传递给你的服务对象以调用所需的云端点

6、您还可以在您的应用引擎代码中添加用户检查以确保有效用户正在访问

from google.appengine.ext import endpoints
current_user = endpoints.get_current_user()
if raise_unauthorized and current_user is None:
raise endpoints.UnauthorizedException('Invalid token.')

因为您有 google+,您还可以使用给定的 userinfo.email 范围查找 emailid here .另请查看这 2 个帖子以获取有用信息:GCE with G+Oauth on GAE with google play services

关于android - Google+ 和 AppEngine 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17132971/

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