Google+和AppEngine身份验证

时间:2013-06-16 11:46:37

标签: android google-app-engine 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();

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

由于

1 个答案:

答案 0 :(得分:5)

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

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

String accountName = mPlusClient.getAccountName();

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

2,将上述步骤中的帐户名称保存到共享首选项和凭据:

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,在具有云端点的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.')

由于您使用的是Google +,因此您还可以使用userinfo.email范围查找emailid here。另请查看这两篇帖子以获取有用信息:GCE with G+Oauth on GAE with google play services