使用谷歌日历 api 时,Android 给 IOException 提供“无法创建目录:/令牌" [英] Android giving IOException with 'unable to create directory: /tokens' when using google calendar api
问题描述
尝试将日历快速入门 API 实施到 Android 中,但是当我按照演示声明令牌时.private final String TOKENS_DIRECTORY_PATH = "tokens";
Trying to implement the Calendar Quickstart API into Android but when I declare tokens as demonstrated.
private final String TOKENS_DIRECTORY_PATH = "tokens";
然后在构建器中使用该字符串
That String is then used in the builder
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
But Android returns this error when setting the DataStoreFactory
java.io.IOException: 无法创建目录:/tokens
是否有不同的方法来创建一个有效的目录?还是我必须更改 TOKENS_DIRECTORY_PATH
的文件路径?
Is there a different method to creating a directory that will work? or must I change the file path of TOKENS_DIRECTORY_PATH
?
推荐答案
我使用了这段代码.
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 清单文件中获取外部存储的权限
And get permissions to external storage in Android manifest file
Google API 文档中指定的 Java 方法似乎不适用于 Android.使用此 github 项目作为将 Google API 集成到 Android 应用程序中的指南.
The methods specified in Google API documentation for Java doesn't seem to work well for Android. Use this github project as a guide for implementing integrating Google APIs into Android applications.
这篇关于使用谷歌日历 api 时,Android 给 IOException 提供“无法创建目录:/令牌"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!