Get gmail profile image and full name

后端 未结 2 1633
故里飘歌
故里飘歌 2021-01-14 14:39

I am preparing a login page for my app. I wanted to do the same thing that twitter is doing. They are helping the user and predefining some fields like email, name and prof

2条回答
  •  终归单人心
    2021-01-14 15:42

    Place below code in your activity. You will get what you wanted. This is new way to fetch google user info.

    //Global variables:
            GoogleSignInOptions gso;
            private GoogleApiClient mGoogleApiClient;    
        //inside onCreate():
            gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                                .requestEmail()
                                .requestProfile()
                                .requestScopes(new Scope(Scopes.PLUS_ME))
                                .requestScopes(new Scope(Scopes.PLUS_LOGIN))
                                .requestScopes(new Scope(Scopes.PROFILE))
                                .build();
                        mGoogleApiClient = new GoogleApiClient.Builder(this)
                                .enableAutoManage(this , this )
                                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                                .addApi(Plus.API)
                                .build();
    

    Place Below code out of onCreate()

     //to get google account info:
        private void handleSignInResult(GoogleSignInResult result) {
                Log.d(TAG, "handleSignInResult:" + result.isSuccess());
                if (result.isSuccess()) {
                    GoogleSignInAccount acct = result.getSignInAccount();
                    String pic_info=null;
                    int g;
                    String gender="Null";
                    String userid="";
                    if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
                        Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
                        userid=currentPerson.getId(); //BY THIS CODE YOU CAN GET CURRENT LOGIN USER ID
                        g=currentPerson.getGender();
                        gender = (g==1)?"Female":(g==0)?"Male":"Others";
                    }
    
                    if(acct.getPhotoUrl() != null) {
                        pic_info = acct.getPhotoUrl().toString();
                        Log.e("info", pic_info + " ");
                    }
                    Toast.makeText(getApplicationContext(),"welcome "+acct.getDisplayName(),Toast.LENGTH_LONG).show();
                    SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("user", MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPref.edit();
                    editor.putString("social_media","google");
                    editor.putString("id",userid);
                    editor.putString("email",acct.getEmail());
                    editor.putString("name",acct.getDisplayName());
                    editor.putString("profile_pic",pic_info);
                    editor.putString("gender",gender);
                    editor.apply();
                    Intent intent=new Intent(LoginActivity.this,SignOutActivity.class);
                    startActivity(intent);
                    finish();
                }
    

提交回复
热议问题