java.io.IOException: Error getting access token for service account: 401 Unauthorized
at com.google.auth.oauth2.ServiceAccountCredentials.refreshAccessToken(ServiceAccountCredentials.java:444)
at com.google.auth.oauth2.OAuth2Credentials.refresh(OAuth2Credentials.java:157)
at com.google.auth.oauth2.OAuth2Credentials.getRequestMetadata(OAuth2Credentials.java:145)
at com.google.auth.oauth2.ServiceAccountCredentials.getRequestMetadata(ServiceAccountCredentials.java:603)
at com.google.auth.http.HttpCredentialsAdapter.initialize(HttpCredentialsAdapter.java:91)
at com.google.api.client.http.HttpRequestFactory.buildRequest(HttpRequestFactory.java:88)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.buildHttpRequest(AbstractGoogleClientRequest.java:430)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:549)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:482)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:599)
at com.soo.gsuite.GmailQuickstart.sendMessage(GmailQuickstart.java:168)
at com.soo.gsuite.GmailQuickstart.main(GmailQuickstart.java:275)
Caused by: com.google.api.client.http.HttpResponseException: 401 Unauthorized
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1094)
at com.google.auth.oauth2.ServiceAccountCredentials.refreshAccessToken(ServiceAccountCredentials.java:441)
... 11 more
代码如下:
public static void main(String... args) throws IOException, GeneralSecurityException {
// Build a new authorized API client service.
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
try {
Gmail service = gmsEmailServiceAccount("yanhen@benexxxackaging.com");
// Directory service2 = getDirectoryService("admin@benexxxackaging.com");
MimeMessage emailContent = createEmail("admin@benexxxackaging.com", "me", "test", "test");
sendMessage(service, "admin@benexxxackaging.com", emailContent);
sendMessage(service, "admin@benexxxackaging.com", emailContent);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Gmail gmsEmailServiceAccount(String userEmail)
throws GeneralSecurityException, IOException, URISyntaxException {
/*
* InputStream resourceAsStream = new
* GmailQuickstart().getClass().getClassLoader()
* .getResourceAsStream(getJsonDataFromPath(userEmail));
*/
InputStream resourceAsStream = GmailQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
OAuth2Credentials credential = ServiceAccountCredentials.fromStream(resourceAsStream).createDelegated(userEmail)
.createScoped(GMSGmailScopesOnly());
HttpCredentialsAdapter adapter = new HttpCredentialsAdapter(credential);
Gmail service = new Gmail.Builder(new NetHttpTransport(), JSON_FACTORY, adapter)
.setApplicationName("Gmail API Java Quickstart").build();
return service;
}
private static MimeMessage createEmail(String to, String from, String subject, String bodyText)
throws MessagingException {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage email = new MimeMessage(session);
email.setFrom(new InternetAddress(from));
email.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
email.setSubject(subject);
email.setText(bodyText);
return email;
}
public static void sendMessage(Gmail service, String userId, MimeMessage email)
throws MessagingException, IOException {
Message message = createMessageWithEmail(email);
message = service.users().messages().send(userId, message).execute();
System.out.println("Message id: " + message.getId());
System.out.println(message.toPrettyString());
}