如何整合谷歌+登录我的Android应用程序? [英] How to integrate google+ sign in my android app?

查看:133
本文介绍了如何整合谷歌+登录我的Android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好所有其实我需要通过我的应用程序登录google +的人,现在我阅读google上的文档,了解它


允许用户登录,将Google登录集成到您的应用中。初始化GoogleApiClient对象时,请求PLUS_LOGIN和PLUS_ME作用域



  mGoogleApiClient =新的GoogleApiClient。 Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Scopes.PLUS_LOGIN)
.addScope (Scopes.PLUS_ME)
.build();

现在我很困惑,应该只使用上面的代码还是先执行谷歌登录在我的onsuccess方法中,编写此代码以从Google +获取个人资料。另外在上面的代码中,Scopes.Scopes.PLUS_LOGIN不起作用,所以我用PLUS.PLUS_LOGIN google的文档太旧了。



现在我已经使用了上面的代码,但它不是仅从Google +帐户登录才会显示一个对话框,表明我需要登录哪个帐户,但是当我点击该帐户时,它不会执行任何操作目前我正在使用谷歌标志集成,请参阅下面的代码

  mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this / * FragmentActivity * /,this / * OnConnectionFailedListener * /)
.addApi(Auth.GOOGLE_SIGN_IN_API,gso)
.build();

和everthing运作良好,但我需要个人的性别和他/她的出生日期为什么我想使用谷歌+但问题是谷歌+集成不适合我现在应该做什么



如果有人知道如何获取人的性别或出生日期使用任何两个谷歌+或谷歌登录API请告诉我如何做到这一点,这可能对我来说真的意味着什么。

解决方案

 这是代码


public class MainActivity extends AppCompatActivity implements View.OnClickListener,GoogleApiClient.OnConnectionFailedListener {private static final String TAG = MainActivity.class.getSimpleName(); private static final int RC_SIGN_IN = 007;私人GoogleApiClient mGoogleApiClient;私人ProgressDialog mProgressDialog;私人按钮btnSignIn;私人按钮btnSignOut;私人LinearLayout llProfileLayout;私人ImageView imgProfilePic;私人TextView txtName,txtEmail; @Override保护无效onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);的setContentView(R.layout.activity_main); btnSignIn =(Button)findViewById(R.id.login); btnSignOut =(Button)findViewById(R.id.logout); imgProfilePic =(ImageView)findViewById(R.id.pic); txtName =(TextView)findViewById(R.id.name); txtEmail =(TextView)findViewById(R.id.email); btnSignIn.setOnClickListener(本); btnSignOut.setOnClickListener(本); GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail()。build(); mGoogleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this,this).addApi(Auth.GOOGLE_SIGN_IN_API,gso).build(); } private void signIn(){Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent,RC_SIGN_IN); } {private void void signOut(){Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(new ResultCallback< Status>(){@Override public void onResult(Status status){updateUI(false);}}); } {private void void revokeAccess(){Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(new ResultCallback< Status>(){@Override public void onResult(Status status){updateUI(false);}}); } private void handleSignInResult(GoogleSignInResult result){Log.d(TAG,handleSignInResult:+ result.isSuccess()); if(result.isSuccess()){/ *已成功登录,显示经过身份验证的用户界面。* / GoogleSignInAccount acct = result.getSignInAccount();
Log.e(TAG,display name:+ acct.getDisplayName());

String personName = acct.getDisplayName();
String personPhotoUrl = acct.getPhotoUrl()。toString();
String email = acct.getEmail(); (TAG,姓名:+ personName +,电子邮件地址:+ email
+,图片:+ personPhotoUrl);


Log.e

txtName.setText(personName);
txtEmail.setText(email);
;
} else {

updateUI(false);
}
}

@Override
public void onClick(View v){
int id = v.getId();

switch(id){
case R.id.login:
signIn();
休息;

case R.id.logout:
signOut();
休息;

$ b}

@Override
public void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult( requestCode,resultCode,data);

if(requestCode == RC_SIGN_IN){GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
Log.d(TAG,onActivityResult:+ result);
handleSignInResult(result);

$ b}

@Override
public void onStart(){
super.onStart();

OptionalPendingResult< GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if(opr.isDone()){
Log.d(TAG,已缓存登录);
GoogleSignInResult result = opr.get();

$ b}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult){

Log.d( TAG,onConnectionFailed:+ connectionResult);



$ b private void updateUI(boolean isSignedIn){
if(isSignedIn){
btnSignIn.setVisibility(View.GONE) ;
btnSignOut.setVisibility(View.VISIBLE);
llProfileLayout.setVisibility(View.VISIBLE);
} else {
btnSignIn.setVisibility(View.VISIBLE);
btnSignOut.setVisibility(View.GONE);




code $ pre

Hello all actually i need to sign in people from google+ through my app now i read documentation on google where it isstated that

To allow users to sign in, integrate Google Sign-In into your app. When you initialize the GoogleApiClient object, request the PLUS_LOGIN and PLUS_ME scopes

mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Scopes.PLUS_LOGIN)
.addScope(Scopes.PLUS_ME)
.build();

Now i am confused that whether should i use above code only or implement google sign-in first and after that in my onsuccess method write this code to fetch person's profile from google+. Also in above code Scopes.Scopes.PLUS_LOGIN was not working so i have used PLUS.PLUS_LOGIN google's docs is so old.

Now i have used above code but it is not signing in from google+ account only it shows a dialog that from which account i need to sign in but when i click on that account it doesn't do anything also currently i am using google-sign integration see below code

mGoogleApiClient = new GoogleApiClient.Builder(this)
    .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
    .build();

and everthing is working great but i need person's gender and his/her date of birth that's why i thought about using google+ but the problem is google+ integration is not working for me what should i do now ??

If anyone know how to fetch person's gender or date of birth using any of the two google+ OR google sign-in api please tell me how can i do this it could really mean something for me.

解决方案

This is the code


  public class MainActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener { private static final String TAG = MainActivity.class.getSimpleName(); private static final int RC_SIGN_IN = 007; private GoogleApiClient mGoogleApiClient; private ProgressDialog mProgressDialog; private Button btnSignIn; private Button btnSignOut; private LinearLayout llProfileLayout; private ImageView imgProfilePic; private TextView txtName, txtEmail; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSignIn = (Button)findViewById(R.id.login); btnSignOut = (Button) findViewById(R.id.logout); imgProfilePic = (ImageView) findViewById(R.id.pic); txtName = (TextView) findViewById(R.id.name); txtEmail = (TextView) findViewById(R.id.email); btnSignIn.setOnClickListener(this); btnSignOut.setOnClickListener(this); GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build(); mGoogleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this, this).addApi(Auth.GOOGLE_SIGN_IN_API, gso).build(); } private void signIn() { Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, RC_SIGN_IN); } private void signOut() { Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(new ResultCallback<Status>() { @Override public void onResult(Status status) { updateUI(false); } }); } private void revokeAccess() { Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(new ResultCallback<Status>() { @Override public void onResult(Status status) { updateUI(false); } }); } private void handleSignInResult(GoogleSignInResult result) { Log.d(TAG, "handleSignInResult:" + result.isSuccess()); if (result.isSuccess()) { /* Signed in successfully, show authenticated UI.*/ GoogleSignInAccount acct = result.getSignInAccount();
    Log.e(TAG, "display name: " + acct.getDisplayName());

    String personName = acct.getDisplayName();
    String personPhotoUrl = acct.getPhotoUrl().toString();
    String email = acct.getEmail();


    Log.e(TAG, "Name: " + personName + ", email: " + email
            + ", Image: " + personPhotoUrl);

    txtName.setText(personName);
    txtEmail.setText(email);
    ;
} else {

    updateUI(false);
}
}

    @Override
    public void onClick(View v) {
        int id = v.getId();

        switch (id) {
            case R.id.login:
                signIn();
                break;

            case R.id.logout:
                signOut();
                break;

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

                if (requestCode == RC_SIGN_IN) {GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            Log.d(TAG, "onActivityResult: "+result);
            handleSignInResult(result);

        }
    }

    @Override
    public void onStart() {
        super.onStart();

        OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
        if (opr.isDone()) {
        Log.d(TAG, "Got cached sign-in");
            GoogleSignInResult result = opr.get();

        }
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

        Log.d(TAG, "onConnectionFailed:" + connectionResult);
    }



    private void updateUI(boolean isSignedIn) {
        if (isSignedIn) {
            btnSignIn.setVisibility(View.GONE);
            btnSignOut.setVisibility(View.VISIBLE);
            llProfileLayout.setVisibility(View.VISIBLE);
        } else {
            btnSignIn.setVisibility(View.VISIBLE);
            btnSignOut.setVisibility(View.GONE);

        }
    }
}

这篇关于如何整合谷歌+登录我的Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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