Google+和AppEngine身份验证

Joa*_*usa 2 google-app-engine android oauth-2.0 google-plus google-cloud-endpoints

我有一个已安装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();
Run Code Online (Sandbox Code Playgroud)

所以这很好,但我正在重新进行身份验证.我可以以任何方式利用PlusClient来避免创建更多凭据吗?

谢谢

ton*_*y m 5

您可以执行以下步骤,使用Google加号对Google AppEngine端点的用户进行身份验证:

1,使用google plus获取帐户名称,该帐户名称将在步骤中

String accountName = mPlusClient.getAccountName();
Run Code Online (Sandbox Code Playgroud)

现在,您不必使用示例代码中提供的帐户选择器,因为您通过google plus输入帐户名称

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

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

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

4,在具有云端点的app引擎项目中,对于@ endpoints.api装饰器或@endpoints.method装饰器的allowed_client_ids参数,提供Android客户端ID和Web客户端ID.您还必须将@ endpoints.api装饰器的audiences参数设置为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.')
Run Code Online (Sandbox Code Playgroud)

由于您有google +,您还可以使用此处给出的userinfo.email范围查找emailid .另请查看这两篇帖子以获取有用信息:GCE与G +Oauth在GAE上使用Google Play服务