Google+ Sign-In on Android Studio- GoogleAuthException: BadUsername -
after sign-in succeeds can't retrieve profile information , keep getting error on googleauthutil.gettoken() method.
the error:
com.google.android.gms.auth.googleauthexception: badusername
on android client id inferred automatically combination of android package name, , sha-1 fingerprint of signing key in developers console project. matched package name , tried of debug.keystore have on workstation. nothing worked.
*the consent screen set (!) does't appear.
the whole working fragment:
public class googleplusactivity extends fragment implements connectioncallbacks, onconnectionfailedlistener { private static final string tag = "signinactivity"; // magic number use know sign-in error // resolution activity has completed. private static final int request_resolve_error = 9000; // core google+ client. private googleapiclient mgoogleapiclient; // flag stop multiple dialogues appearing 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 flag whether should trigger // resolution of connectionfailed connectionresult. mresolvingerror = false; log.i(tag, "signing in..."); } @override public void onstart() { super.onstart(); log.i(tag, "start"); // every time start want try connect. if // succeeds we'll onconnected() callback. if // fails we'll onconnectionfailed(), result! if (!mresolvingerror) { mgoogleapiclient.connect(); } } @override public void onstop() { super.onstop(); // can little costly keep connection open // google play services, each time our activity // stopped should disconnect. log.i(tag, "stop"); mgoogleapiclient.disconnect(); } @override public void onconnectionfailed(connectionresult result) { log.i(tag, "connectionfailed : " + result.tostring()); if (mresolvingerror) { // attempting resolve error. return; } else if (result.hasresolution()) { try { mresolvingerror = true; result.startresolutionforresult(this.getactivity(), request_resolve_error); } catch (sendintentexception e) { // there error 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 flag, if user signs out they'll have // tap sign in again. mresolvingerror = false; if(mtoken == null || mtoken.isempty()) { // retrieve 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) { // call not ever expected succeed // assuming have verified // google play services 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 app not connected or attempting connect if (!mgoogleapiclient.isconnecting() && !mgoogleapiclient.isconnected()) { mgoogleapiclient.connect(); } } } } @override public void onconnectionsuspended(int arg0) { // onconnectionsuspended log.i(tag, "onconnectionsuspended."); } }
what doing wrong?
can post return value plus.accountapi.getaccountname(mgoogleapiclient)
?
also make sure have in manifest:
<uses-permission android:name="android.permission.get_accounts" />
Comments
Post a Comment