When using the Gmail API, I want to use a service account to simulate my email and operate my inbox, thereby bypassing the user’s manual authorization operations. Is it possible to implement the postgraduate entrance examination? Can anyone help me?
there is my code
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountPrivateKeyFromP12File(GmailServiceAccountExample.class.getResourceAsStream(CREDENTIALS_FILE_PATH))
.setServiceAccountScopes(Collections.singletonList(GmailScopes.MAIL_GOOGLE_COM))
.setServiceAccountUser(USER_EMAIL)
.build();
But encountered the following problem:
com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized POST https://oauth2.googleapis.com/token at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:103) at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:308) at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:324) at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:378) at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:470) at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:201) 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 cn.hzj.gamil.example.GmailServiceAccountExample.listMessages(GmailServiceAccountExample.java:73) at cn.hzj.gamil.example.GmailServiceAccountExample.main(GmailServiceAccountExample.java:39)
ZiJIng He is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.