I've been searching to find a solution for my problem for days without result. The answers I found come close to what I want, but it's not enough to make it actually work.
What do I want?
I want to give the users of my program the possibility to send an email using their own Gmail account after giving my program that privilege with oauth2. My program will then send an email, using that account, on certain conditions (programatically) to the desired receivers (this can also be yourself). This means, after the user is giving permission using oauth2, my program will then make the decision when to email the desired receivers inclusive the contents of that email. This is of course only in the best interest of the user who will be fully aware of this functionality when he/she turns it on.
What have I done so far?
Of course, I have turned on the Gmail API at console.developers.google.com and tried different kind of credentials (native/android ...)
I've been reading and trying and reading and trying...
https://developers.google.com/gmail/api/
Handling expired access token in Android without user concent
looking for Android Gmail SMTP Oauth example
How to Login into Gmail using OAuth in Android Application?
https://developers.google.com/accounts/docs/OAuth2
http://developer.android.com/google/auth/http-auth.html
https://developers.google.com/api-client-library/java/apis/gmail/v1
https://developers.google.com/api-client-library/java/google-api-java-client/oauth2
https://developers.google.com/accounts/docs/OAuth2ForDevices
Plus the first 20 like Google results for every combination of search criteria using the words, Android, Gmail, oauth, email, send etc.
The code I used is nowhere different from the sources I used, it is possible to select a google account in my program (app). Below is a part of the piece of (mind the ugliness at this point) code after an account has been selected. Depending on what part of the code I use (uncomment) I get different error codes, like 403: access not configured or 401: unauthorized
@Override
protected Void doInBackground(Void... params) {
String token = null;
try {
token = GoogleAuthUtil.getToken(activity, userId, scope);
if (token != null) {
//Log.e("token", token);
// send test mail
HttpTransport transport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
// load secret
//Reader secret = new InputStreamReader(activity.getAssets().open("secret.json"));
//GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory, secret);
// add scopes
//Collection<String> scopes = new ArrayList<String>();
//scopes.add(OAuthScopes.GMAIL.COMPOSE.getLocation());
//GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, clientSecrets, scopes)
// .setAccessType("online").setApprovalPrompt("auto").build();
//String url = flow.newAuthorizationUrl().setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI).build();
//GoogleCredential credential = new GoogleCredential().setAccessToken(token);
Reader secret = new InputStreamReader(activity.getAssets().open("secret.json"));
GoogleCredential credential = new GoogleCredential.Builder().setClientSecrets(GoogleClientSecrets.load(jsonFactory, secret))
.build();
Gmail gm = new Gmail.Builder(transport, jsonFactory, credential).setApplicationName("Test").build();
//Gmail gm = new Gmail(httpTrans, jsf, null);
Properties properties = new Properties();
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.starttls.required", "true");
properties.put("mail.smtp.sasl.enable", "false");
// etc
Hope someone can help me out here!