I am trying to authenticate a service account to Google to send emails from a server application.
this is done by
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static HttpTransport HTTP_TRANSPORT;
private static final List<String> SCOPES =
Arrays.asList(GmailScopes.GMAIL_LABELS, GmailScopes.GMAIL_SEND);
return new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("xxxxxxxxxxxxxx.iam.gserviceaccount.com")
.setServiceAccountScopes(SCOPES)
.setServiceAccountPrivateKeyFromP12File(new File("C:/temp/google/secret.p12"))
.build();
The server response I am getting is
com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Bad Request",
"reason" : "failedPrecondition"
} ],
"message" : "Bad Request"
}
On my service account I have enabled the option "Enable G Suite Domain-wide Delegation"
Is the above the correct way to try and authenticate, or have i missed a step