【问题标题】:G+ connection failed statusCode=SIGN_IN_REQUIREDG+ 连接失败 statusCode=SIGN_IN_REQUIRED
【发布时间】:2016-10-04 15:35:47
【问题描述】:

为什么我会收到此错误?我已经在 Google Developer Console 中创建了一个项目,启用了 Drive API 和 Google+ API,并使用我签名的 apk 的 sha1 添加了“Android Client Id”(顺便说一下,我已经在 Google Play Console 上作为 beta 共享)。

我获得了权限 GET_ACCOUNTS,当我在列表中选择我的 G+ 邮件时,我收到了以下错误:

 D/G+: Connection failed4ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent

我必须在我的项目文件夹中的某处添加 client_id.json 吗? (我刚刚在 Google Developer Console 中为 Drive API 创建了它。)

代码:

      // initialize g+ api client
    if (Sp.getBoolean("plus_pic", false)) {

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .build();
    }

和:

 @Override
public void onConnected(Bundle bundle) {

    if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
        Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);

        String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient);
        Person.Image personImage;
        Person.Cover.CoverPhoto personCover;

        try {

            personImage = currentPerson.getImage();
            personCover = currentPerson.getCover().getCoverPhoto();
        } catch (Exception e) {
            Log.e("G+ COnnection error"," ->"+e.toString());
            personCover = null;
            personImage = null;
        }

        if (personCover != null && personImage != null) {

            String imgUrl = personImage.getUrl();
            Log.e("g+ connection","cover and pic not null");
            // getting full size image
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(imgUrl);
            stringBuilder.delete(imgUrl.length() - 6, imgUrl.length());
            Log.d("G+", stringBuilder.toString());
            mGoogleName.setText(currentPerson.getDisplayName());
            mGoogleId.setText(accountName);
            // setting cover pic
            ImageLoader.getInstance().loadImage(personCover.getUrl(), displayImageOptions, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    super.onLoadingComplete(imageUri, view, loadedImage);
                    drawerHeaderParent.setBackgroundColor(Color.parseColor("#ffffff"));
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        drawerHeaderView.setBackground(new BitmapDrawable(loadedImage));
                    } else
                        drawerHeaderView.setBackgroundDrawable(new BitmapDrawable(loadedImage));

                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    super.onLoadingFailed(imageUri, view, failReason);
                    drawerHeaderView.setBackgroundResource(R.drawable.app_header);
                    drawerHeaderParent.setBackgroundColor(Color.parseColor((currentTab == 1 ? skinTwo : skin)));
                }

                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    super.onLoadingStarted(imageUri, view);
                    drawerHeaderView.setBackgroundResource(R.drawable.app_header);
                    drawerHeaderParent.setBackgroundColor(Color.parseColor((currentTab == 1 ? skinTwo : skin)));
                }
            });

            // setting profile pic
            ImageLoader.getInstance().loadImage(stringBuilder.toString(), displayImageOptions, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    super.onLoadingComplete(imageUri, view, loadedImage);
                    drawerProfilePic.setImageBitmap(loadedImage);
                    drawerProfilePic.setVisibility(View.VISIBLE);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    super.onLoadingFailed(imageUri, view, failReason);
                }
            });


        } else {
            Toast.makeText(this, getResources().getText(R.string.no_cover_photo), Toast.LENGTH_SHORT).show();
            drawerHeaderView.setBackgroundResource(R.drawable.app_header);
            drawerHeaderParent.setBackgroundColor(Color.parseColor((currentTab == 1 ? skinTwo : skin)));
        }
    }
}

@Override
public void onConnectionSuspended(int i) {

    Log.d("G+", "Connection suspended");
    new Thread(new Runnable() {
        @Override
        public void run() {
            if (mGoogleApiClient != null) {

                mGoogleApiClient.connect();
            }
        }
    }).run();
}

public void onConnectionFailed(final ConnectionResult result) {
    Log.d("G+", "Connection failed"+ result.getErrorCode()+result.toString());

    if (!mIntentInProgress && result.hasResolution()) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    mIntentInProgress = true;
                    startIntentSenderForResult(result.getResolution().getIntentSender(),
                            RC_SIGN_IN, null, 0, 0, 0);
                } catch (IntentSender.SendIntentException e) {
                    // The intent was canceled before it was sent.  Return to the default
                    // state and attempt to connect to get an updated ConnectionResult.
                    mIntentInProgress = false;
                    if (mGoogleApiClient != null) {

                        mGoogleApiClient.connect();
                    }
                }
            }
        }).run();
    }
}

和:

protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
    if (requestCode == RC_SIGN_IN && !mGoogleApiKey && mGoogleApiClient != null) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                mIntentInProgress = false;
                mGoogleApiKey = true;
                // !mGoogleApiClient.isConnecting
                if (mGoogleApiClient.isConnecting()) {
                    mGoogleApiClient.connect();
                } else
                    mGoogleApiClient.disconnect();

            }
        }).run();
    }

【问题讨论】:

    标签: android google-drive-api google-plus


    【解决方案1】:

    您收到错误SIGN_IN_REQUIRED,因为客户端尝试连接到服务但用户未登录。

    客户端可以选择不使用 API 继续。或者,如果hasResolution() 返回 true,客户端可能会调用 [startResolutionForResult(Activity, int)](https://developers.google.com/android/reference/com/google/android/gms/common/ConnectionResult.html#startResolutionForResult(android.app.Activity, int)) 来提示用户登录。在登录活动返回 RESULT_OK 后,进一步的尝试应该会成功。

    根据此forum,尝试通过从您的 gmail 帐户转到 Google API 控制台来解决此问题。在那里,您会在 Google Drive API 前面看到一个禁用按钮。您将看到一个齿轮或设置按钮,单击它并生成 oAuth 令牌。

    以下是一些相关的主题:

    希望这会有所帮助!

    【讨论】:

    • 我已经生成了 auth2 令牌,并将 google_services.json 导入到“app/”文件夹,但出现此错误。我必须在代码中使用该 client_id 吗?
    • 顺便说一句,我检查了你的链接,没有答案我认为很多人都面临这个问题。
    【解决方案2】:

    由于我的原因,我错误地使用了发布 SHA1 密钥在开发者控制台中生成 API。然后使用 debug.keystore 创建 SHA1 密钥并在我的 api 凭据中更新。它开始工作了。

    keytool -list -v -keystore "C:\Users\<user>n\.android\debug.keystore" -alias androiddebugkey -storepass andro
    id -keypass android
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-15
      • 2014-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多