google api - Android how can I take the access token from the authServerCode? -
so googleapiclient:
gso = new googlesigninoptions.builder(googlesigninoptions.default_sign_in) .requestemail() .requestserverauthcode(serverid) .build(); mgoogleapiclient = new googleapiclient.builder(pssigninflowactivity.this) .enableautomanage(this/* fragmentactivity */, this) .addapi(auth.google_sign_in_api, gso) .build();
this happens when press login button:
public void login(){ log.i("", "handlesigninresult login:"); intent signinintent = auth.googlesigninapi.getsigninintent(mgoogleapiclient); startactivityforresult(signinintent, rc_sign_in); }
which takes me here:
@override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); log.i("", "handlesigninresult onactivityresult:" + requestcode + ".." + resultcode); // result returned launching intent googlesigninapi.getsigninintent(...); if (requestcode == rc_sign_in) { googlesigninresult result = auth.googlesigninapi.getsigninresultfromintent(data); log.i("","handlesigninresult onactivityresult: " +result.getsigninaccount() + ",,," + result.getstatus()); handlesigninresult(result); } }
which calls this:
private void handlesigninresult(googlesigninresult result) { log.i("", "handlesigninresult:" + result.issuccess()); if (result.issuccess()) { // signed in successfully, show authenticated ui. googlesigninaccount acct = result.getsigninaccount(); string authcode = acct.getserverauthcode(); // if(authcode != null) // storyfragment.setfromgoogle(authcode); } else { // signed out, show unauthenticated ui. } }
my issue need accesstoken googlesigninresult, serverauthcode. saw can exchange serverauthcode accesstoken on auth2 playground. can done programmatically?
this works me:
try{ string scope = "oauth2:" + scopes.profile; account account = new account(acct.getemail(), "com.google"); final string token = googleauthutil.gettoken(pssigninflowactivity.this, account, scope); runonuithread(new runnable() { @override public void run() { storyfragment.setfromgoogle(token); } }); }catch (exception e){ log.e("","error trying client secret : " + e.getmessage()); }
Comments
Post a Comment