如何使用GoogleApiClient为云端点客户端提供凭据 [英] How to use GoogleApiClient to provide credentials for cloud endpoint client

查看:136
本文介绍了如何使用GoogleApiClient为云端点客户端提供凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,使用Google Play服务的GoogleApiClient我已经让用户选择一个帐户(如果有多个)并确认我的Android应用的oauth权限。我需要这个排行榜和其他一些东西。

但我也在使用AppEngine后端,并需要使用该端口来验证用户身份。要做到这一点,我需要通过emailAccount进行身份验证。



在集成Google Play服务之前,我手动处理了帐户选择,因此我始终可以访问emailAccount由用户选择。但是GoogleApiClient可以处理这种情况。

  private void signInToGoogleAccount(){
googleApiClient = new GoogleApiClient.Builder这个)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)// Profile + Circles +?writing应用活动?
.build();
googleApiClient.connect();
}

// GPS连接回调。

@Override
public void onConnected(Bundle bundle){
Log.i(TAG,#onConnected - GoogleApiClient connected !!);
if(Plus.PeopleApi.getCurrentPerson(googleApiClient)!= null){
final Person currentPerson = Plus.PeopleApi.getCurrentPerson(googleApiClient);
String personName = currentPerson.getDisplayName();
Log.i(TAG,name =+ personName);
String personGooglePlusProfile = currentPerson.getUrl();
Log.i(TAG,profileUrl =+ personGooglePlusProfile);



@Override
public void onConnectionSuspended(int cause){
Log.d(TAG,#onConnectionSuspended - GoogleApiClient连接暂停。 cause =+ cause);
googleApiClient.connect();
}

@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == RC_SIGN_IN){
Log.i(TAG,#onActivityResult RC_SIGN_IN resultCode =+ resultCode +data =+ data);
intentInProgress = false;
if(resultCode == RESULT_OK){
if(!googleApiClient.isConnecting()){
googleApiClient.connect();
}
} else if(resultCode == RESULT_CANCELED){
Log.i(TAG,#onActivityResult RC_SIGN_IN user cancelled);
} else {
Log.w(TAG,#onActivityResult RC_SIGN_IN something weird);



$ b private void doRemoteTask(){
final GoogleAccountCredential凭证= GoogleAccountCredential.usingAudience(context,AppConstants.AUDIENCE);

//这是我需要从GooglePlayServices登录中恢复的信息。
credential.setSelectAccountName(userAccountName);

MyRemoteTask myRemoteTask = new MyRemoteTask.Builder(
AndroidHttp.newCompatibleTransport(),new GsonFactory(),凭证
).build();
myRemoteTask.doThing(someArg);
}

所以我需要知道的是:


  1. 当我使用GoogleApiClient进行授权时,如何获取用户选择的电子邮件帐户的详细信息。

  2. 更多用GoogleApiClient友好的方式将用户身份注入到AppEngine Cloud Endpoints客户端。



解决方案是使用 Plus.AccountApi.getAccountName(googleApiClient);
例如

  @Override 
public void onConnected(Bundle bundle){
final String accountName = Plus.AccountApi.getAccountName(googleApiClient);
Log.i(TAG,#onConnected - GoogleApiClient accountName =+ accountName);
}


Ok, using the GoogleApiClient of Google Play Services I have let the user select an account (if multiple) and confirm oauth permissions for my Android app. I need this for leader boards and a few other things.

But I'm also using an AppEngine backend and need to authenticate the user with that. To do so I need to pass in the emailAccount with which to authenticate.

Prior to integrating Google Play Services I handled account selection manually, so I always had access to the emailAccount selected by the user. But the GoogleApiClient handles that at arms length.

private void signInToGoogleAccount() {
    googleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(Plus.API)
        .addScope(Plus.SCOPE_PLUS_LOGIN) // Profile + Circles + ?writing app activities?
        .build();
    googleApiClient.connect();
}

// GPS Connection Callbacks.

@Override
public void onConnected(Bundle bundle) {
    Log.i(TAG, "#onConnected - GoogleApiClient connected!!");
    if (Plus.PeopleApi.getCurrentPerson(googleApiClient) != null) {
        final Person currentPerson = Plus.PeopleApi.getCurrentPerson(googleApiClient);
        String personName = currentPerson.getDisplayName();
        Log.i(TAG, "name=" + personName);
        String personGooglePlusProfile = currentPerson.getUrl();
        Log.i(TAG, "profileUrl=" + personGooglePlusProfile);
    }
}

@Override
public void onConnectionSuspended(int cause) {
    Log.d(TAG, "#onConnectionSuspended - GoogleApiClient connection suspend. cause=" + cause);
    googleApiClient.connect();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        Log.i(TAG, "#onActivityResult RC_SIGN_IN resultCode=" + resultCode + " data=" + data);
        intentInProgress = false;
        if (resultCode == RESULT_OK) {
            if (!googleApiClient.isConnecting()) {
                googleApiClient.connect();
            }
        } else if (resultCode == RESULT_CANCELED) {
            Log.i(TAG, "#onActivityResult RC_SIGN_IN user cancelled");
        } else {
            Log.w(TAG, "#onActivityResult RC_SIGN_IN something weird");
        }
    }
}

private void doRemoteTask() {
    final GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(context, AppConstants.AUDIENCE);

    // This is the info that I need to recover from the GooglePlayServices signin.
    credential.setSelectAccountName(userAccountName);

    final MyRemoteTask myRemoteTask = new MyRemoteTask.Builder(
        AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential
    ).build();
    myRemoteTask.doThing(someArg);
}

So what I need to know is :

  1. How do I get the details of the email account selected by the user when I use GoogleApiClient to authorize.
  2. Or is there another more GoogleApiClient friendly way of injecting user identity into an AppEngine Cloud Endpoints client.

解决方案

Amazing how writing the question out for others can help unblock thought processes.

Solution is use Plus.AccountApi.getAccountName(googleApiClient); Eg

@Override
public void onConnected(Bundle bundle) {
    final String accountName = Plus.AccountApi.getAccountName(googleApiClient);
    Log.i(TAG, "#onConnected - GoogleApiClient accountName=" + accountName);
}

这篇关于如何使用GoogleApiClient为云端点客户端提供凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆