Android:如何将google plus登录到android应用程序中?

时间:2017-01-09 11:38:03

标签: android google-play-services google-plus

我想将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;
    }*/

}

1 个答案:

答案 0 :(得分:0)

这可能是您的Google控制台(或Firebase)设置中的一个问题。

此外,您应该考虑使用新的Google登录Android API。在这里,您可以找到分步指南:

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

Google Plus登录已被弃用,不再受支持(参考下文)。

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