google play services 8.3.0 - 获取g +封面图片(不推荐使用Plus.PeopleApi.getCurrentPerson())

时间:2015-11-16 16:51:27

标签: android google-play-services google-signin

如何使用google play服务登录g +后获取封面图片?

使用之前的播放服务版本Plus.PeopleApi.getCurrentPerson().getCover()工作正常,但现在不推荐使用此方法,Plus.PeopleApi.getCurrentPerson()返回null。我要求个人资料范围和Plus.API

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestProfile()
        //.requestIdToken()
        .requestEmail()
        .requestScopes(Plus.SCOPE_PLUS_LOGIN, Plus.SCOPE_PLUS_PROFILE, new Scope("https://www.googleapis.com/auth/plus.profile.emails.read"))
        .build();
// [END configure_signin]

// [START build_client]
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this)
        .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
        .addApi(Plus.API)
        .build();

谢谢!

2 个答案:

答案 0 :(得分:0)

您是否添加了OAuth密钥?同时创建OAuth常量屏幕。我在添加此

后才得到结果

答案 1 :(得分:-1)

您好我找到了最新google plus登录方式以下方法:

[#current]
== [[historic1]][[historic2]]Caption

活动结果上使用以下代码: -

  GoogleApiClient mGoogleApiClient;

private void latestGooglePlus() {
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestProfile().requestEmail().requestScopes(Plus.SCOPE_PLUS_LOGIN, Plus.SCOPE_PLUS_PROFILE, new Scope("https://www.googleapis.com/auth/plus.profile.emails.read"))
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .addApi(Plus.API)
            .build();


    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, YOURREQUESTCODE);
}

最后你的onclick将是: -

   @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.e("Activity Res", "" + requestCode);
    if (requestCode == YOURREQUESTCODE) {

        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
             GoogleSignInAccount acct = result.getSignInAccount();
            acct.getPhotoUrl();
            acct.getId();
            Log.e(TAG, acct.getDisplayName());
            Log.e(TAG, acct.getEmail());


            Plus.PeopleApi.load(mGoogleApiClient, "signed_in_user_account_id").setResultCallback(new ResultCallback<People.LoadPeopleResult>() {
                @Override
                public void onResult(@NonNull People.LoadPeopleResult loadPeopleResult) {
                     PersonBuffer personBuffer = loadPeopleResult.getPersonBuffer();
                    if (personBuffer != null && personBuffer.getCount() > 0) {
                        Person currentUser = personBuffer.get(0);
                        personBuffer.release();
                        Person.Cover cover = currentUser.getCover();
                        if (cover != null) {
                            Person.Cover.CoverPhoto coverPhoto = cover.getCoverPhoto();
                            if (coverPhoto != null) {
                                String userCoverPhotoUrl = coverPhoto.getUrl();
                                Log.i("Main TAG", "Cover photo Url :" + userCoverPhotoUrl);

                            }
                        } else {
                            Log.i("TAG NO COVER", "Person has no cover");
                        }
                    }
            });
        }


    }  
}