简体   繁体   English

Android:如何将Google Plus登录集成到Android应用程序中?

[英]Android: How to integrate google plus sign in into android application?

I want to integrate google plus sign in into my android application i have tried almost all the source code which are they in net but still i am unable to implement i d'not have any errors in my code but still it will not log in through gmail please any one give the solution. 我想将google plus登录集成到我的android应用程序中,我已经尝试了几乎所有源代码,它们都在网络中,但是我仍然无法实现我的代码没有任何错误,但仍然无法通过登录gmail请任何人提供解决方案。

mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(Plus.API, Plus.PlusOptions.builder().build()).addScope(Plus.SCOPE_PLUS_LOGIN).build();

    }


     protected void onStart() {
            super.onStart();
            mGoogleApiClient.connect();
          }


     protected void onStop() {
            super.onStop();
            if (mGoogleApiClient.isConnected()) {
              mGoogleApiClient.disconnect();
            }
          }


     private void resolveSignInError() {
            if (mConnectionResult.hasResolution()) {
              try {
                mIntentInProgress = true;
                mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
              } catch (SendIntentException e) {
                mIntentInProgress = false;
                mGoogleApiClient.connect();
              }
            }
          }


     @Override
      public void onConnectionFailed(ConnectionResult result) {
        if (!result.hasResolution()) {
          GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
          return;
        }

        if (!mIntentInProgress) {
          // store mConnectionResult
          mConnectionResult = result;

          if (signedInUser) {
            resolveSignInError();
          }
        }
      }

      @Override
      protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
        switch (requestCode) {
        case RC_SIGN_IN:
          if (responseCode == RESULT_OK) {
            signedInUser = false;

          }
          mIntentInProgress = false;
          if (!mGoogleApiClient.isConnecting()) {
            mGoogleApiClient.connect();
          }
          break;
        }
      }

      @Override
      public void onConnected(Bundle arg0) {
        signedInUser = false;
        Toast.makeText(this, "Connected", Toast.LENGTH_LONG).show();
        getProfileInformation();
      }

      private void updateProfile(boolean isSignedIn) {
        if (isSignedIn) {
          signinFrame.setVisibility(View.GONE);
          profileFrame.setVisibility(View.VISIBLE);

        } else {
          signinFrame.setVisibility(View.VISIBLE);
          profileFrame.setVisibility(View.GONE);
        }
      }

      private void getProfileInformation() {
        try {
          if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
            Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
            String personName = currentPerson.getDisplayName();
            String personPhotoUrl = currentPerson.getImage().getUrl();
            String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

            username.setText(personName);
            emailLabel.setText(email);

            new LoadProfileImage(image).execute(personPhotoUrl);

            // update profile frame with new info about Google Account
            // profile
            updateProfile(true);
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }

      @Override
      public void onConnectionSuspended(int cause) {
        mGoogleApiClient.connect();
        updateProfile(false);
      }

      @Override
      public void onClick(View v) {
        switch (v.getId()) {
        case R.id.signin:
          googlePlusLogin();
          break;
        }
      }

      public void signIn(View v) {
        googlePlusLogin();
      }

      public void logout(View v) {
        googlePlusLogout();
      }

      private void googlePlusLogin() {
        if (!mGoogleApiClient.isConnecting()) {
          signedInUser = true;
          resolveSignInError();
        }
      }

      private void googlePlusLogout() {
        if (mGoogleApiClient.isConnected()) {
          Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
          mGoogleApiClient.disconnect();
          mGoogleApiClient.connect();
          updateProfile(false);
        }
      }

      // download Google Account profile image, to complete profile
      private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
        ImageView downloadedImage;

        public LoadProfileImage(ImageView image) {
          this.downloadedImage = image;
        }

        protected Bitmap doInBackground(String... urls) {
          String url = urls[0];
          Bitmap icon = null;
          try {
            InputStream in = new java.net.URL(url).openStream();
            icon = BitmapFactory.decodeStream(in);
          } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
          }
          return icon;
        }

        protected void onPostExecute(Bitmap result) {
          downloadedImage.setImageBitmap(result);
        }


      }
    /*@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }*/

}

It's probably an issue from your Google Console (or Firebase) setup. 您的Google控制台(或Firebase)设置中可能存在此问题。

Also you should consider using the new Google Sign-In Android API. 另外,您应该考虑使用新的Google登录Android API。 Here you can find step-by-step guide: 您可以在这里找到分步指南:

https://developers.google.com/+/mobile/android/getting-started https://developers.google.com/+/mobile/android/getting-started

Google Plus Sign-In has been deprecated and will not be supported any further (reference below). Google Plus登录已被弃用,以后将不再受支持(以下参考)。

https://developers.google.com/+/mobile/android/api-deprecation https://developers.google.com/+/mobile/android/api-deprecation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM