我可以连接到gmail api,&可以阅读标签。但当我尝试阅读消息时,我有一个错误403(ListMessagesResponse listMessageResponse = service.users().messages().list("me").execute()
)
`{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Insufficient Permission",
"reason" : "insufficientPermissions"
} ],
"message" : "Insufficient Permission"
}`
我获取服务的代码:
`final String CLIENT_ID = "////";
final String CLIENT_SECRET = "///////";
final String APPLICATION_NAME = "///////////////";
final String REFRESH_TOKEN = "////////////////";
final java.io.File DATA_STORE_DIR = new java.io.File("/", "");
FileDataStoreFactory DATA_STORE_FACTORY;
final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
final List<Set<String>> SCOPES = Arrays.asList(GmailScopes.all());
HttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
Credential credential = new GoogleCredential.Builder()
.setClientSecrets(CLIENT_ID, CLIENT_SECRET)
.setJsonFactory(JSON_FACTORY).setTransport(HTTP_TRANSPORT).build()
.setRefreshToken(REFRESH_TOKEN);
credential.refreshToken();
return new Gmail.Builder(
HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();`
我的获取消息的代码(我希望获取所有未读消息并保存附件)
`ListMessagesResponse listMessageResponse = service.users().messages()
.list("me").execute();
List<Message> list = listMessageResponse.getMessages();
for (Message mess : list) {
System.out.println(mess.getRaw());
}`
答案 0 :(得分:0)
在您执行授权调用之前,您似乎正在尝试刷新令牌。
查看gmail api示例中的authorize()方法:
https://developers.google.com/gmail/api/quickstart/java#step_3_set_up_the_sample