无法为Google云端硬盘创建令牌目录
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法为Google云端硬盘创建令牌目录相关的知识,希望对你有一定的参考价值。
我正在尝试从android应用程序连接到Google云端硬盘,最终将从云端硬盘下载音轨。我无法创建/ token /目录进行身份验证。
我已经分拆了创建GoogleAuthorizationCodeFlow
的过程,以显示失败前该过程的进度。我还试图预先创建/ token /目录,这也失败了。
这是相关的代码。由于我使用的是内部存储,因此未在清单文件中请求任何权限。
@Override
public void onReceive(Context context, Intent intent) {
Log.i("LOGLEARN", "Starting AlarmReceiver");
Drive service;
try {
HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();
service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName("Morning Learning")
.build();
FileList result = service.files().list()
.setPageSize(10)
.setFields("nextPageToken, files(id, name)")
.execute();
List<File> files = result.getFiles();
if (files == null || files.isEmpty()) {
Log.i(DEBUG,"No files found.");
} else {
Log.i(DEBUG, "Files:");
for (File file : files) {
Log.i(DEBUG, "%s (%s) - " + file.getName() + file.getId());
}
}
} catch (Exception exception) {
// Handle Exception here
Log.i(DEBUG, "Exception: " + exception.toString());
}
}
private static Credential getCredentials(final HttpTransport HTTP_TRANSPORT) throws IOException {
String DEBUG = "LOGLEARN";
// Load client secrets.
InputStream in = AlarmReceiver.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
java.io.File tokenFolder = new java.io.File(Environment.getDataDirectory() +
java.io.File.separator + TOKENS_DIRECTORY_PATH);
if (!tokenFolder.exists()) {
Log.i(DEBUG, "Token Folder does not exist " + tokenFolder.toString());
tokenFolder.mkdirs();
}
if (!tokenFolder.exists()) {
Log.i(DEBUG, "Token Folder still does not exist " + tokenFolder.toString());
} else {
Log.i(DEBUG, "Token Folder now exists " + tokenFolder.toString());
}
GoogleAuthorizationCodeFlow.Builder build = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES);
Log.i(DEBUG, "Checkpoint 1");
build.setDataStoreFactory(new FileDataStoreFactory(tokenFolder));
Log.i(DEBUG, "Checkpoint 2");
build.setAccessType("offline");
Log.i(DEBUG, "Checkpoint 3");
GoogleAuthorizationCodeFlow flow = build.build();
Log.i(DEBUG, "Checkpoint end");
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
我希望得到一些写入LogCat的文件列表,但我得到以下内容:
I/LOGLEARN: Starting AlarmReceiver
I/LOGLEARN: Token Folder does not exist /data/tokens
I/LOGLEARN: Token Folder still does not exist /data/tokens
I/LOGLEARN: Checkpoint 1
I/LOGLEARN: Exception: java.io.IOException: unable to create directory: /data/tokens
发生了两件我不明白的奇怪事情。
- 它在这条线上失败了
build.setDataStoreFactory(new FileDataStoreFactory(tokenFolder));
tokenFolder.mkdirs();
没有按预期创建目录,也没有抛出异常。
另外,我不理解类名GoogleAuthorizationCodeFlow.Builder
的术语 - 这只是一个带有句号的类名,还是对内部类或其他内容的引用?
答案
我希望你最终得到这个“创建/令牌/目录”问题的答案。
如果情况变得更糟,您只需要访问您知道其Google云端硬盘路径的MP3文件...
- 示例:https://drive.google.com/file/d/XXXXX/view
- 成为(通过使用
String
函数编辑): https://drive.google.com/uc?authuser=0&id=XXXXX&export=download
仅使用您的文件ID替换XXXXX
。 MP3字节现在将下载,因此要么读入bytearray
,要么在MP3解码器/播放器中用作URL。
以上是关于无法为Google云端硬盘创建令牌目录的主要内容,如果未能解决你的问题,请参考以下文章