我尝试从我的应用程序(Spring Boot应用程序)连接到Gmail API,以便下载我的一些邮件。它确实有效,但我有授权问题。当我调用authorize()方法时,它在IDE控制台中给我一个auth链接。有没有办法将用户重定向到授权页面,而不是在控制台中打印此链接?
我的代码:
public Credential authorize() throws IOException
{
InputStream inputStream = GTD.class.getResourceAsStream("/client_secret.json");
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(inputStream));
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;
}
public Gmail getGmailService() throws IOException
{
Credential credential = authorize();
return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName("GTD")
.build();
}
编辑:当我从普通的Java应用程序而不是Web应用程序调用函数时,它可以工作。