I am trying to send a Gmail with Google-API for Android.
For that, I authenticated the current user as instructed by Google documentation:
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build()
val mGoogleSignInClient = GoogleSignIn.getClient(this, gso)
val signInIntent = mGoogleSignInClient.signInIntent
startActivityForResult(signInIntent, 1)
...
// Pardon the quick and dirty coding, it's a POC.
override fun onActivityResult... {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
val account = task.getResult()
}
Apologies for the deprecated code, but I needed something for quick ramp-up and I don’t have better tidy sources
Then, I send the mail
val credentials = GoogleAccountCredential.usingOAuth2(context, listOf(GmailScopes.GMAIL_SEND))
val service = Gmail.Builder(
NetHttpTransport(),
GsonFactory.getDefaultInstance(),
credentials
)
.setApplicationName("GMailPlayground") // Does it need to be consistent with anything?
.build()
// Valid because I logged the user in
val account = getAccount() ?: throw IllegalStateException("No account found")
val mimeMessage = CreateMessage.createEmail(
toEmailAddress = "[email protected]",
fromEmailAddress = account.email,
subject = "Test",
bodyText = "This is a test"
)
val message = CreateMessage.createMessageWithEmail(mimeMessage)
val task = service.users().messages().send(account.id, message)
task.execute()
Where createEmail
and createMessageWithEmail
are the snippets from Google documentation (https://developers.google.com/gmail/api/guides/sending). They just create a Base64 message.
I keep getting the following exception:
Process: com.rycbar.gmailplayground, PID: 2114
java.lang.IllegalArgumentException: the name must not be empty: null
at android.accounts.Account.<init>(Account.java:85)
at android.accounts.Account.<init>(Account.java:70)
at com.google.android.gms.auth.zzl.getToken(com.google.android.gms:play-services-auth-base@@18.0.10:5)
at com.google.android.gms.auth.GoogleAuthUtil.getToken(com.google.android.gms:play-services-auth-base@@18.0.10:3)
at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:258)
at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:283)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:880)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:525)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:466)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:576)
at com.rycbar.gmailplayground.auth.GmailHandler.sendEmail(GmailHandler.kt:57)
Now, I see that it has to do with authentication, but the user is authenticated. The app is a simple Android app so – the user has their account set.
What am I missing? Google exception won’t tell me what “name” is empty. Is it a “name/data” map? Is it an authentication detail that’s missing?
Did I misconfigure the app in some Google console?
Do I have to create a fully production app with a review process in Google APIs just to send GMail messages from Android devices?
Thanks in advance
P.S: Please, if I missed something silly, please provide an explanation. I have been running around this for hours.