首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google+登录

Google+登录
EN

Stack Overflow用户
提问于 2016-06-06 17:07:22
回答 4查看 192关注 0票数 4

我需要通过我的应用程序的谷歌加登录,我得到了除Lolly流行的所有版本的登录。通过我的应用程序登录google plus的流程是什么?请给我答复什么是实际的程序…

这是我到目前为止所做的代码。

代码语言:javascript
复制
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SyncStateContract;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.OptionalPendingResult;
import com.google.android.gms.common.api.ResultCallback;


public class GmailSignin extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener{
    TextView signInButton;
    private GoogleApiClient mGoogleApiClient;
    private static final int RC_SIGN_IN = 9001;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();

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


    signInButton = (TextView) findViewById(R.id.sign_in_button);
    signInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            signIn();
        }
    });
}


 void signIn() {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

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

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        handleSignInResult(result);
    }
}

private void handleSignInResult(GoogleSignInResult result) {
    Log.d("Signin", "handleSignInResult:" + result.isSuccess());
    if (result.isSuccess()) {
        // Signed in successfully, show authenticated UI.
        GoogleSignInAccount acct = result.getSignInAccount();
        Conatants.name=acct.getDisplayName();
        Conatants.email=acct.getEmail();
        Conatants.id=acct.getId();
        Intent intent=new Intent(getApplicationContext(),Details.class);
        startActivity(intent);

    } else {
        // Signed out, show unauthenticated UI.
    }
}


@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}
public void onStart(){
    OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);

    if (opr.isDone()) {
        // If the user's cached credentials are valid, the OptionalPendingResult will be "done"
        // and the GoogleSignInResult will be available instantly.
        Log.d("gmailsignup", "Got cached sign-in");
        GoogleSignInResult result = opr.get();
        handleSignInResult(result);
    } else {
        // If the user has not previously signed in on this device or the sign-in has expired,
        // this asynchronous branch will attempt to sign in the user silently.  Cross-device
        // single sign-on will occur in this branch.
        opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
            @Override
            public void onResult(GoogleSignInResult googleSignInResult) {
                handleSignInResult(googleSignInResult);
            }
        });
    }
}
}
EN

回答 4

Stack Overflow用户

发布于 2016-06-06 18:06:28

我从来没有用Facebook login google plus登录过,但我很习惯它的模块来帮助你。祝你在here上好运。

票数 0
EN

Stack Overflow用户

发布于 2016-06-06 18:08:16

(我正在尝试格式化代码大约一个小时,但没有运气,有人编辑正确的格式的答案请)。

实际答案:

这对我来说很有效,目前它正在生产中,所以它经过了彻底的测试

公共类LoginActivity扩展了FragmentActivity实现的View.OnClickListener,GoogleApiClient.OnConnectionFailedListener {

代码语言:javascript
复制
private GoogleApiClient mGoogleApiClient;
    
private static final int RC_SIGN_IN = 9001;

private Button signUpGoogle;

@Override
    protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_login);

    signUpGoogle = (Button) findViewById(R.id.sign_up_google);
        signUpGoogle.setOnClickListener(this);

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                                .requestEmail()
                                .build();

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

@Override
    public void onClick(View v) {
        signInGoogle();
    }

private void signInGoogle() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

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

//     Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleSignInResultGoogle(result);
        }
    }

    private void handleSignInResultGoogle(GoogleSignInResult result) {
        AlertsAndLogs.log(TAG, "handleSignInResult:" + result.isSuccess());
        if (result.isSuccess()) {
            GoogleSignInAccount acct = result.getSignInAccount();
            if (acct != null) {
                //Get details from account: acc and do something when them
            }
        } else {
            AlertsAndLogs.toast("SignUp with Google failed " + result.toString());
        }
    }

@Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        AlertsAndLogs.logError(TAG, "onConnectionFailed:" + connectionResult);
    }

}

票数 0
EN

Stack Overflow用户

发布于 2016-06-06 18:17:11

这是我在我的应用程序中用来登录Google plus的一个修改版本。适用于我支持的所有api版本(14及以上)。

简而言之,这个类执行以下操作。

当结果为登录按钮的结果时,

  • GoogleApiClient设置为所需的作用域。当按钮为created.
  • Registers clicked.
  • Handles onActivityResult.

中的登录结果时,Activity

  • 将使用结果启动登录Activtiy

SignInActivity.java

代码语言:javascript
复制
    public class SignInActivity extends AppCompatActivity implements
            GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener {
        private static final int SIGN_IN_REQUEST = 101;

        private GoogleApiClient mGoogleApiClient;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_sign_in);
            initialize();
        }

        @Override
        public void onConnected(@Nullable Bundle bundle) {
            // Maybe only enable the sign in button as soon as the api is connected
        }

        @Override
        public void onConnectionSuspended(int i) {
            // Check best practices
        }

        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
            // Check best practices
        }

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

            if (requestCode == SIGN_IN_REQUEST) {

                final AppPreference preference = PreferenceFactory.getPreferenceHelper(this);
                GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
                if (result != null && result.isSuccess()) {
                    // DO SOMETHING ON SUCCESS
                } else {
                    // HANDLE FAILED LOGIN
                }
            }
        }

        protected void signInClicked() {

            //SHOW A PROGRESS BAR OR SOME INDICATION OF SIGN IN
            Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
            startActivityForResult(signInIntent, SIGN_IN_REQUEST);
        }

        private void initialize() {

            setupGoogleApiClient();
            setupToolbar();
            setupSignInButton();
        }

        private Toolbar setupToolbar() {

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.setDisplayHomeAsUpEnabled(true);
                actionBar.setTitle(getString(R.string.sign_in_activity));
            }

            return toolbar;
        }

        private void setupSignInButton() {

            findViewById(R.id.signInButton).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    signInClicked();
                }
            });
        }

        private void setupGoogleApiClient() {

            GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestProfile()
                    .requestEmail()
                    .requestScopes(Drive.SCOPE_FILE, Drive.SCOPE_APPFOLDER)
                    .build();

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

            mGoogleApiClient.connect();
        }
    }

有关这方面的更多信息,请查看登录Android上的this教程。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37653469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档