如何使用AWS Lambda授权gmail API

时间:2019-02-18 17:28:17

标签: java amazon-web-services gmail-api

所以我试图通过aws lambda从gmail帐户获取所有邮件。 当我在本地测试时,一切正常。 当我上传Lambda时说 “ java.io.IOException:无法创建目录令牌”

我使用以下代码:

 private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        // Load client secrets.
        InputStream in = TlvPartnersClient.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
        return new AuthorizationCodeInstalledApp(flow, receiver).authorize(TLV_PARTNER_MAIL);
    }

1 个答案:

答案 0 :(得分:0)

Lamdbda Functions运行在Amazon Linux之上,其唯一可写目录为/ tmp。

您的TOKENS_DIRECTORY_PATH绝对没有指向/ tmp,因此是java.io.IOException。如果TOKENS_DIRECTORY_PATH是某种Windows路径(C:*),则该路径也不起作用。

还请记住,Lambda函数中的临时存储限制为512MB。

https://docs.aws.amazon.com/lambda/latest/dg/limits.html