android - Gmail API: Authentication using token retrieval -
i have read whole article authentication using oauth2.0. didn't find suitable method on android application. please suggest method access token can build gmail service object , access inbox or other method.
this example given them in link:
googlecredential credential = new googlecredential().setaccesstoken(accesstoken); plus plus = new plus.builder(new nethttptransport(), jacksonfactory.getdefaultinstance(), credential) .setapplicationname("google-plussample/1.0") .build();
invoke below method token , google account used in mobile. method first retrieves google account setup in mobile , later retrieves token. can save token , account name using preferences later use dont have retrieve token each time.
private void chooseaccount() { intent intent = accountpicker.newchooseaccountintent(null, null, new string[]{"com.google"}, false, null, null, null, null); startactivityforresult(intent, 9009); }
after account retrieved below method called,
public static final string mail_google_com = "https://mail.google.com"; public static final string gmail_compose = "https://www.googleapis.com/auth/gmail.compose"; public static final string gmail_modify = "https://www.googleapis.com/auth/gmail.modify"; private static final string scope = "oauth2:" + gmail_compose + " " + gmail_modify + " " + mail_google_com; @override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (resultcode == activity.result_ok) { string accountname = data.getstringextra(accountmanager.key_account_name); //accountname - google account in mobile retrieved //now use google account retrieve token new gettoken(getactivity().getapplicationcontext(), scope, accountname).execute(); showerrordialog(exception); } } else if (requestcode == activity.result_canceled) { toast.maketext(getactivity(), "cancelled!!!", toast.length_short).show(); } }
below class used token.
private class gettoken extends asynctask<void, void, void> { context context; string mscope, memail, token; gettoken(context context, string scope, string email) { this.context = context; this.mscope = scope; this.memail = email; } @override protected void doinbackground(void... params) { try { token = googleauthutil.gettoken(context, memail, mscope); //save token using preference later use or stuff using token here log.v("ranjapp", "token " + token); } catch (userrecoverableauthexception e) { handleexception(e); } catch (googleauthexception ex) { handleexception(ex); } catch (exception e) { //display error dialog } return null; } void handleexception(final exception e) { getactivity().runonuithread(new runnable() { @override public void run() { if (e instanceof userrecoverableauthexception) { intent intent = ((userrecoverableauthexception) e).getintent(); startactivityforresult(intent, 10098); } else if (e instanceof googleplayservicesavailabilityexception) { int statuscode = ((googleplayservicesavailabilityexception) e) .getconnectionstatuscode(); dialog dialog = googleplayservicesutil.geterrordialog(statuscode, getactivity(), 10099); dialog.show(); } } }); } }
you have register app in google play console token successfully. ensure have play services setup in app.
to register android app google cloud console:
- visit google cloud console.
- if have existing project you're adding android app, select project. otherwise, click create project @ top, enter project name , id, click create. note: name provide project name appears users in google settings app in list of connected apps.
- in left-side navigation, select apis & auth.
- enable api you'd use setting status on.
- in left-side navigation, select credentials.
- click create new client id or create new key appropriate app.
- complete form appears filling in android app details. sha1 fingerprint app, run following command in terminal:
keytool -exportcert -alias <keystore_alias> -keystore <keystore_path> -list -v
example, you're using debug-key eclipse, command looks this:keytool -exportcert -alias androiddebugkey-keystore ~/.android/debug.keystore -list -v
keystore password "android". - click create.
for more information: https://developer.android.com/google/auth/http-auth.html
Comments
Post a Comment