将社交登录(Google 和 Facebook)添加到现有的 Android 登录

问题描述 投票:0回答:1

我有一个现有的 Android 应用程序,您可以创建帐户并通过用户名/电子邮件和密码登录,现在我希望能够创建帐户并使用 Google 或 Facebook 等社交平台登录。

我的 Android 应用程序通过我们制作的 NodeJS API 连接到数据库。此 API 有一个路由 xxxxx/authenticate,您可以在其中发送电子邮件/用户名和密码,它会返回一个令牌。

我的问题是如何将 Google 和 Facebook 登录添加到我们现有的设置中并添加会话管理。

我尝试这样做这个并且我已经能够通过Google登录,但我的问题是我的所有API查询都受到令牌保护,我现在被困在这里。

我不知道是否必须修改 API 才能接受来自 Google 和 Facebook 的令牌,或者尝试在应用程序级别处理所有问题。

关于此事的任何意见或建议将不胜感激。

谢谢。

android node.js facebook-login google-signin
1个回答
1
投票

这是谷歌加的

您需要在您的项目中添加一些权限和库。

public class MainActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {

        private SignInButton signInButton;
        private GoogleSignInOptions gso;
        private GoogleApiClient mGoogleApiClient;
        private int RC_SIGN_IN = 100;
        private TextView textViewName;
        private TextView textViewEmail;
        private NetworkImageView profilePhoto;
        private ImageLoader imageLoader;

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

            textViewName = (TextView) findViewById(R.id.textViewName);
            textViewEmail = (TextView) findViewById(R.id.textViewEmail);
    //        profilePhoto = (NetworkImageView) findViewById(R.id.profileImage);
            gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
            signInButton = (SignInButton) findViewById(R.id.sign_in_button);
            signInButton.setSize(SignInButton.SIZE_WIDE);
            signInButton.setScopes(gso.getScopeArray());
            mGoogleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */).
                    addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                    .build();
            signInButton.setOnClickListener(this);
        }
        private void signIn() {
            Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
            startActivityForResult(signInIntent, RC_SIGN_IN);
        }
        public void sign_out(View view) {
            Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
                    new ResultCallback<Status>() {
                        @Override
                        public void onResult(Status status) {
                            Toast.makeText(getApplicationContext(), "SIGN OUT SUCESSFULLY", Toast.LENGTH_LONG).show();
                            textViewName.setText("");
                            textViewEmail.setText("");
                        }
                    });
    //        Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
    //                new ResultCallback<Status>() {
    //                    @Override
    //                    public void onResult(Status status) {
    //                        // ...
    //                    }
    //                });

        }
        @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);
                handleSignInResult(result);
            }
        }

        private void handleSignInResult(GoogleSignInResult result) {
            if (result.isSuccess()) {
                GoogleSignInAccount acct = result.getSignInAccount();
                textViewName.setText(acct.getDisplayName());
                textViewEmail.setText(acct.getEmail());
    //            Toast.makeText(this,acct.get)

                //Initializing image loader
    //            imageLoader = CustomVolleyRequest.getInstance(this.getApplicationContext())
    //                    .getImageLoader();
    //
    //            imageLoader.get(acct.getPhotoUrl().toString(),
    //                    ImageLoader.getImageListener(profilePhoto,
    //                            R.mipmap.ic_launcher,
    //                            R.mipmap.ic_launcher));
    //
    //            //Loading image
    //            profilePhoto.setImageUrl(acct.getPhotoUrl().toString(), imageLoader);

            } else {
                Toast.makeText(this, "Login Failed", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onClick(View v) {
            if (v == signInButton) {
                signIn();
            }
        }

        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {

        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.