Google Plus Connection始终返回SIGN_IN_FAILED

时间:2015-05-05 10:31:34

标签: android login google-plus

我试图在我的应用程序中执行Google Plus登录,但无论我做什么,如果Scope设置为Plus.SCOPE_PLUS_LOGIN,我总是得到一个ErrorRode为17(SIGN_IN_FAILED)的ConnectionResult。另一方面,如果我选择Plus.SCOPE_PLUS_PROFILE,应用程序将连接,但是当我检索Person对象时,此对象始终为null。

我在开发者控制台中创建了一个新项目,启用了Google+ API并创建了一个Cliend ID,其中包含适用于该应用及其软件包的调试版本的正确SHA1指纹。我还选了一个电子邮件和产品名称。

enter image description here

我还尝试重命名应用程序包并在开发人员控制台中更新项目,但我仍然遇到同样的错误。

问题是我已经下载了Google示例,在我的开发者控制台中为它创建了一个项目并且它可以工作......但是当我尝试使用在我的应用程序中执行该作业的同一个类时,它总是失败。 .. 真的很奇怪。我还检查了Google + API使用情况部分,它从不处理我的应用发送的任何请求...而它是为Google示例执行的...

这是执行整个过程的类的代码..正如您所看到的,它看起来几乎与Google示例中的相同......

public class MainActivity extends FragmentActivity implements
    GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
    ResultCallback<People.LoadPeopleResult>, View.OnClickListener {

private static final int STATE_DEFAULT = 0;
private static final int STATE_SIGN_IN = 1;
private static final int STATE_IN_PROGRESS = 2;

private static final int RC_SIGN_IN = 0;

private static final int DIALOG_PLAY_SERVICES_ERROR = 0;

private static final String SAVED_PROGRESS = "sign_in_progress";

private int mSignInError;

private SignInButton mSignInButton;
private Button mSignOutButton;
private Button mRevokeButton;
private TextView mStatus;
private ListView mCirclesListView;
private ArrayAdapter<String> mCirclesAdapter;
private ArrayList<String> mCirclesList;

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

    mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
    mSignOutButton = (Button) findViewById(R.id.sign_out_button);
    mRevokeButton = (Button) findViewById(R.id.revoke_access_button);
    mStatus = (TextView) findViewById(R.id.sign_in_status);
    mCirclesListView = (ListView) findViewById(R.id.circles_list);

    mSignInButton.setOnClickListener(this);
    mSignOutButton.setOnClickListener(this);
    mRevokeButton.setOnClickListener(this);

    mCirclesList = new ArrayList<String>();
    mCirclesAdapter = new ArrayAdapter<String>(
            this, R.layout.circle_member, mCirclesList);
    mCirclesListView.setAdapter(mCirclesAdapter);

    if (savedInstanceState != null) {
        mSignInProgress = savedInstanceState
                .getInt(SAVED_PROGRESS, STATE_DEFAULT);
    }

    mGoogleApiClient = buildGoogleApiClient();
}

private GoogleApiClient buildGoogleApiClient() {
    return new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API, Plus.PlusOptions.builder().build())
                    //.addScope(Plus.SCOPE_PLUS_PROFILE)
            .addScope(Plus.SCOPE_PLUS_LOGIN)
            .build();
}

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

@Override
protected void onStop() {
    super.onStop();

    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt(SAVED_PROGRESS, mSignInProgress);
}

@Override
public void onClick(View v) {
    if (!mGoogleApiClient.isConnecting()) {
        switch (v.getId()) {
            case R.id.sign_in_button:
                mStatus.setText("Sign in");
                resolveSignInError();
                break;
            case R.id.sign_out_button:
                Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
                mGoogleApiClient.disconnect();
                mGoogleApiClient.connect();
                break;
            case R.id.revoke_access_button:
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient);
                mGoogleApiClient = buildGoogleApiClient();
                mGoogleApiClient.connect();
                break;
        }
    }
}

@Override
public void onConnected(Bundle connectionHint) {
    Log.i(TAG, "onConnected");

    mSignInButton.setEnabled(false);
    mSignOutButton.setEnabled(true);
    mRevokeButton.setEnabled(true);

    Person currentUser = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);

    Log.d("profile", "profile, name: " + currentUser.getName() + ", id: " + currentUser.getId());

    String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

    mStatus.setText(String.format("Sign in",
            email));

    Plus.PeopleApi.loadVisible(mGoogleApiClient, null)
            .setResultCallback(this);

    mSignInProgress = STATE_DEFAULT;
}

@Override
public void onConnectionFailed(ConnectionResult result) {

    Log.i(TAG, "onConnectionFailed: ConnectionResult.getErrorCode() = "
            + result.getErrorCode());

    if (result.getErrorCode() == ConnectionResult.API_UNAVAILABLE) {
  } else if (mSignInProgress != STATE_IN_PROGRESS) {
        mSignInIntent = result.getResolution();
        mSignInError = result.getErrorCode();

        if (mSignInProgress == STATE_SIGN_IN) {
            resolveSignInError();
        }
    }

    onSignedOut();
}

private void resolveSignInError() {
    if (mSignInIntent != null) {
        try {
            mSignInProgress = STATE_IN_PROGRESS;
            startIntentSenderForResult(mSignInIntent.getIntentSender(),
                    RC_SIGN_IN, null, 0, 0, 0);
        } catch (IntentSender.SendIntentException e) {
            Log.i(TAG, "Sign in intent could not be sent: "
                    + e.getLocalizedMessage());
            mSignInProgress = STATE_SIGN_IN;
            mGoogleApiClient.connect();
        }
    } else {
        showDialog(DIALOG_PLAY_SERVICES_ERROR);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent data) {
    switch (requestCode) {
        case RC_SIGN_IN:
            if (resultCode == RESULT_OK) {
                mSignInProgress = STATE_SIGN_IN;
            } else {
                mSignInProgress = STATE_DEFAULT;
            }

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

@Override
public void onResult(People.LoadPeopleResult peopleData) {
    if (peopleData.getStatus().getStatusCode() == CommonStatusCodes.SUCCESS) {
        mCirclesList.clear();
        PersonBuffer personBuffer = peopleData.getPersonBuffer();
        try {
            int count = personBuffer.getCount();
            for (int i = 0; i < count; i++) {
                mCirclesList.add(personBuffer.get(i).getDisplayName());
            }
        } finally {
            personBuffer.close();
        }

        mCirclesAdapter.notifyDataSetChanged();
    } else {
        Log.e(TAG, "Error requesting visible circles: " + peopleData.getStatus());
    }
}

private void onSignedOut() {
    mSignInButton.setEnabled(true);
    mSignOutButton.setEnabled(false);
    mRevokeButton.setEnabled(false);

    mStatus.setText("Sign out");

    mCirclesList.clear();
    mCirclesAdapter.notifyDataSetChanged();
}

@Override
public void onConnectionSuspended(int cause) {
    ConnectionResult that we can attempt to resolve.
    mGoogleApiClient.connect();
}

0 个答案:

没有答案