首页 文章

使用google calendar api时,Android会使用'unable to create directory: /tokens'给出IOException

提问于
浏览
0

尝试将Calendar Quickstart API实现到Android中,但是当我声明令牌时 . private final String TOKENS_DIRECTORY_PATH = "tokens";

然后在构建器中使用该String

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

但是设置 DataStoreFactory 时Android会返回此错误

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

是否有不同的方法来创建一个有效的目录?或者我必须更改 TOKENS_DIRECTORY_PATH 的文件路径吗?

1 回答

  • 0

    我用了这段代码 .

    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清单文件中外部存储的权限

    EDIT: Google的Google API文档中指定的方法似乎不适用于Android . 使用此github project作为实施将Google API集成到Android应用程序中的指南 .

相关问题