试图在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
的文件路径?
我使用了这段代码。
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项目作为实现将GoogleAPI集成到Android应用程序的指南。