简体   繁体   English

Google+在Android Studio上登录 - GoogleAuthException:BadUsername

[英]Google+ Sign-In on Android Studio- GoogleAuthException: BadUsername

After Sign-in succeeds I can't retrieve any profile information and keep getting an error on GoogleAuthUtil.getToken() method. 登录成功后,我无法检索任何配置文件信息,并继续在GoogleAuthUtil.getToken()方法上收到错误。

The error: 错误:

com.google.android.gms.auth.GoogleAuthException: BadUsername

On Android client ID is inferred automatically from the combination of the Android package name, and the SHA-1 fingerprint of the signing key in the developers console project. 在Android上,客户端ID是从Android软件包名称和开发人员控制台项目中签名密钥的SHA-1指纹的组合自动推断的。 I matched the package name and even tried all of the debug.keystore I have on my workstation. 我匹配了包名,甚至尝试了我工作站上的所有debug.keystore。 Nothing worked. 没有任何效果。

*The consent screen is set (!) but does't appear. *同意屏幕设置(!)但不会出现。

The whole working Fragment: 整个工作片段:

public class GooglePlusActivity extends Fragment implements ConnectionCallbacks, OnConnectionFailedListener {

private static final String TAG = "SignInActivity";

// magic number use to know that sign-in error
// resolution activity has completed.
private static final int REQUEST_RESOLVE_ERROR = 9000;

// core Google+ client.
private GoogleApiClient mGoogleApiClient;

// flag to stop multiple dialogues appearing for the user.
private boolean mResolvingError;

//TOKEN 
protected String mToken;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG, "Create");

    Plus.PlusOptions options = new Plus.PlusOptions.Builder()
      .addActivityTypes("http://schemas.google.com/AddActivity",
        "http://schemas.google.com/ReviewActivity")
      .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this.getActivity())
      .addApi(Plus.API, options)
      .addConnectionCallbacks(this)
      .addOnConnectionFailedListener(this)
      .build();

    // use mResolveOnFail as a flag to say whether we should trigger
    // the resolution of a connectionFailed ConnectionResult.
    mResolvingError = false;
    Log.i(TAG, "Signing in...");
}

@Override
public void onStart() {
    super.onStart();
    Log.i(TAG, "Start");
    // Every time we start we want to try to connect. If it
    // succeeds we'll get an onConnected() callback. If it
    // fails we'll get onConnectionFailed(), with a result!
    if (!mResolvingError)
    {
        mGoogleApiClient.connect();
    }
}

@Override
public void onStop() {
    super.onStop();
    // It can be a little costly to keep the connection open
    // to Google Play Services, so each time our activity is
    // stopped we should disconnect.
    Log.i(TAG, "Stop");
    mGoogleApiClient.disconnect();
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    Log.i(TAG, "ConnectionFailed : " + result.toString());

    if (mResolvingError) {
        // Already attempting to resolve an error.
        return;
    } else if (result.hasResolution()) {
        try {
            mResolvingError = true;
            result.startResolutionForResult(this.getActivity(), REQUEST_RESOLVE_ERROR);
        } catch (SendIntentException e) {
            // There was an error with the resolution intent. Try again.
            mGoogleApiClient.connect();
        }
    } else {
        // Show dialog using GooglePlayServicesUtil.getErrorDialog()
        Log.i(TAG,"Error num- " + result.getErrorCode());
        mResolvingError = true;
    }


}

//String scope = "oauth2:" + Scopes.PLUS_LOGIN;
//Plus.AccountApi.getAccountName(mGoogleApiClient)
@Override
public void onConnected(Bundle bundle) {
    Log.i(TAG, "Connected.");

    // Turn off the flag, so if the user signs out they'll have to
    // tap to sign in again.
    mResolvingError = false;

    if(mToken == null || mToken.isEmpty())
    {
        // Retrieve the oAuth 2.0 access token.
        final Context context = this.getActivity();

        final String scope = "oauth2:"+ Scopes.PLUS_LOGIN;

        AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                String token = null;

                try {
                    token = GoogleAuthUtil.getToken(
                            context,
                            Plus.AccountApi.getAccountName(mGoogleApiClient),
                            scope);
                } catch (IOException transientEx) {
                    // Network or server error, try later
                    Log.e(TAG, transientEx.toString());
                } catch (UserRecoverableAuthException e) {
                    // Recover (with e.getIntent())
                    Log.e(TAG, e.toString());
                    Intent recover = e.getIntent();
                    startActivityForResult(recover, REQUEST_RESOLVE_ERROR);
                } catch (GoogleAuthException authEx) {
                    // The call is not ever expected to succeed
                    // assuming you have already verified that 
                    // Google Play services is installed.
                    Log.e(TAG, authEx.toString());
                }

                return token;
        }

        @Override
        protected void onPostExecute(String token) {
            mToken = token;
            Toast.makeText(context, "GoogleToken: "+ token, Toast.LENGTH_LONG).show();     
            Log.v(TAG, "Access token retrieved:" + token);
        }

    };
    task.execute();
    }
}

public void onDisconnected() {

    Log.i(TAG, "Disconnected.");
}

public void onActivityResult(int requestCode, int responseCode, Intent intent) {
    Log.i(TAG, "ActivityResult: " + requestCode);
    if (requestCode == REQUEST_RESOLVE_ERROR) {
        mResolvingError = false;
        if (responseCode == GooglePlusActivity.CAUSE_SERVICE_DISCONNECTED) {
            // Make sure the app is not already connected or attempting to connect
            if (!mGoogleApiClient.isConnecting() && !mGoogleApiClient.isConnected()) {
                mGoogleApiClient.connect();
            }
        }
    }
}

@Override
public void onConnectionSuspended(int arg0) {
    // onConnectionSuspended
    Log.i(TAG, "onConnectionSuspended.");
} }

What am I doing wrong? 我究竟做错了什么?

Can you post the return value from Plus.AccountApi.getAccountName(mGoogleApiClient) ? 你能从Plus.AccountApi.getAccountName(mGoogleApiClient)发布返回值吗?

Also make sure you have this in your manifest: 还要确保你的清单中有这个:

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

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

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