首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google Plus在Android上注册?

Google Plus在Android上注册?
EN

Stack Overflow用户
提问于 2014-05-27 09:39:42
回答 2查看 1.6K关注 0票数 1

下面是我正在用Google Plus进行登录的代码。发生了一些内部错误。我想知道我在代码中做错了什么。

使用SCOPE:SCOPE_PLUS_PROFILE时,登录成功,但无法获取用户的个人详细信息。我想做登录使用谷歌和整合获取用户详细信息。如果有人有谷歌的工作代码加登录,那么请帮助我?

我尝试过此链接的方法。另外,我已经在谷歌开发者控制台上注册了这个项目。

代码语言:javascript
复制
      public class MainActivity extends Activity implements OnClickListener,
                ConnectionCallbacks, OnConnectionFailedListener {

            private static final int RC_SIGN_IN = 0;
            // Logcat tag
            private static final String TAG = "MainActivity";

            // Profile pic image size in pixels
            private static final int PROFILE_PIC_SIZE = 400;

            // Google client to interact with Google API
            private GoogleApiClient mGoogleApiClient;

            /**
             * A flag indicating that a PendingIntent is in progress and prevents us
             * from starting further intents.
             */
            private boolean mIntentInProgress;

            private boolean mSignInClicked;

            private ConnectionResult mConnectionResult;

            private SignInButton btnSignIn;
            private Button btnSignOut, btnRevokeAccess;
            private ImageView imgProfilePic;
            private TextView txtName, txtEmail;
            private LinearLayout llProfileLayout;

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

                btnSignIn = (SignInButton) findViewById(R.id.btn_sign_in);
                btnSignOut = (Button) findViewById(R.id.btn_sign_out);
                btnRevokeAccess = (Button) findViewById(R.id.btn_revoke_access);
                imgProfilePic = (ImageView) findViewById(R.id.imgProfilePic);
                txtName = (TextView) findViewById(R.id.txtName);
                txtEmail = (TextView) findViewById(R.id.txtEmail);
                llProfileLayout = (LinearLayout) findViewById(R.id.llProfile);

                // Button click listeners
                btnSignIn.setOnClickListener(this);
                btnSignOut.setOnClickListener(this);
                btnRevokeAccess.setOnClickListener(this);

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

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

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

            /**
             * Method to resolve any signin errors
             * */
            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 the ConnectionResult for later usage
                    mConnectionResult = result;

                    if (mSignInClicked) {
                        // The user has already clicked 'sign-in' so we attempt to
                        // resolve all
                        // errors until the user is signed in, or they cancel.
                        resolveSignInError();
                    }
                }

            }

            @Override
            protected void onActivityResult(int requestCode, int responseCode,
                    Intent intent) {
                if (requestCode == RC_SIGN_IN) {
                    if (responseCode != RESULT_OK) {
                        mSignInClicked = false;
                    }

                    mIntentInProgress = false;

                    if (!mGoogleApiClient.isConnecting()) {
                        mGoogleApiClient.connect();
                    }
                }
            }

            @Override
            public void onConnected(Bundle arg0) {
                mSignInClicked = false;
                Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();

                // Get user's information
                getProfileInformation();

                // Update the UI after signin
                updateUI(true);

            }

            /**
             * Updating the UI, showing/hiding buttons and profile layout
             * */
            private void updateUI(boolean isSignedIn) {
                if (isSignedIn) {
                    btnSignIn.setVisibility(View.GONE);
                    btnSignOut.setVisibility(View.VISIBLE);
                    btnRevokeAccess.setVisibility(View.VISIBLE);
                    llProfileLayout.setVisibility(View.VISIBLE);
                } else {
                    btnSignIn.setVisibility(View.VISIBLE);
                    btnSignOut.setVisibility(View.GONE);
                    btnRevokeAccess.setVisibility(View.GONE);
                    llProfileLayout.setVisibility(View.GONE);
                }
            }

            /**
             * Fetching user's information name, email, profile pic
             * */
            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 personGooglePlusProfile = currentPerson.getUrl();
                        String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

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

                        txtName.setText(personName);
                        txtEmail.setText(email);

                        // by default the profile url gives 50x50 px image only
                        // we can replace the value with whatever dimension we want by
                        // replacing sz=X
                        personPhotoUrl = personPhotoUrl.substring(0,
                                personPhotoUrl.length() - 2)
                                + PROFILE_PIC_SIZE;

                        new LoadProfileImage(imgProfilePic).execute(personPhotoUrl);

                    } else {
                        Toast.makeText(getApplicationContext(),
                                "Person information is null", Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onConnectionSuspended(int arg0) {
                mGoogleApiClient.connect();
                updateUI(false);
            }

            @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;
            }

            /**
             * Button on click listener
             * */
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                case R.id.btn_sign_in:
                    // Signin button clicked
                    signInWithGplus();
                    break;
                case R.id.btn_sign_out:
                    // Signout button clicked
                    signOutFromGplus();
                    break;
                case R.id.btn_revoke_access:
                    // Revoke access button clicked
                    revokeGplusAccess();
                    break;
                }
            }

            /**
             * Sign-in into google
             * */
            private void signInWithGplus() {
                if (!mGoogleApiClient.isConnecting()) {
                    mSignInClicked = true;
                    resolveSignInError();
                }
            }

            /**
             * Sign-out from google
             * */
            private void signOutFromGplus() {
                if (mGoogleApiClient.isConnected()) {
                    Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
                    mGoogleApiClient.disconnect();
                    mGoogleApiClient.connect();
                    updateUI(false);
                }
            }

            /**
             * Revoking access from google
             * */
            private void revokeGplusAccess() {
                if (mGoogleApiClient.isConnected()) {
                    Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
                    Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient)
                            .setResultCallback(new ResultCallback<Status>() {
                                @Override
                                public void onResult(Status arg0) {
                                    Log.e(TAG, "User access revoked!");
                                    mGoogleApiClient.connect();
                                    updateUI(false);
                                }

                            });
                }
            }

            /**
             * Background Async task to load user profile picture from url
             * */
            private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
                ImageView bmImage;

                public LoadProfileImage(ImageView bmImage) {
                    this.bmImage = bmImage;
                }

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

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

        }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-09 22:48:37

改变这一点:

代码语言:javascript
复制
.addOnConnectionFailedListener(this).addApi(Plus.API, null)

对此:

代码语言:javascript
复制
.addOnConnectionFailedListener(this).addApi(Plus.API)

为我工作过。

票数 0
EN

Stack Overflow用户

发布于 2014-07-15 06:11:41

对不起,如果这可能没有帮助,但我想我可能有一个解决方案,可以帮助你走上正确的道路,以修复它。

我刚刚解决了我自己的问题,我有下面的清单给大家:

  • 确保您正在使用debug.keystore SHA-散列或您希望使用的任何密钥存储库。
  • 确保清单文件和类文件中的包名与开发人员控制台中的包名称完全相同。
  • 检查google开发人员控制台是否有正确的APIs供您使用。
  • 在开发人员控制台中的同意书屏幕部分填写基本所需数据
  • 如果您的代码中有逻辑问题,请查看官方建议的实现是如何从教程站点中获得的,或者进一步查看github链接(这里)。
  • 尝试在eclipse/您的IDE和developer控制台中重新编译和重新生成项目。

关于内部错误的简短解释表明,在API中实现Google是不正确的,更多信息(这里)。我自己的问题是基于我在解决代码中的连接时使用的逻辑。我首先重新构建了我的项目,然后更改了实现,在使用API时发现了一些小错误,并对它们进行了更正以使其运行。希望这对你也有帮助。

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

https://stackoverflow.com/questions/23886020

复制
相关文章

相似问题

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