Android在使用谷歌日历API时给IOException带有'无法创建目录:/令牌'

时间:2018-12-10 10:16:18

标签: android google-api google-calendar-api

尝试在Android中实现Calendar Quickstart API,但当我声明令牌时如所示。 private final String TOKENS_DIRECTORY_PATH = "tokens";

然后在构建器中使用该字符串

GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();

但是Android在设置DataStoreFactory

时返回此错误

java.io.IOException: unable to create directory: /tokens

是否有其他方法可以创建目录?还是必须更改TOKENS_DIRECTORY_PATH的文件路径?

1 个答案:

答案 0 :(得分:1)

我用了这段代码。

File tokenFolder = new File(Environment.getExternalStorageDirectory() +
            File.separator + TOKENS_DIRECTORY_PATH);
    if (!tokenFolder.exists()) {
        tokenFolder.mkdirs();
    }

    flow = new GoogleAuthorizationCodeFlow.Builder(
            HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(new FileDataStoreFactory(tokenFolder))
            .setAccessType("offline")
            .build();

并获得Android清单文件中外部存储的权限

编辑:在Google API文档中为Java指定的方法似乎不适用于Android。将此github project用作将Google API集成到Android应用程序中的指南。