gpt4 book ai didi

android - 无法为 Google 云端硬盘创建 token 目录

转载 作者:行者123 更新时间:2023-11-29 02:20:52 25 4
gpt4 key购买 nike

我正在尝试从最终会从云端硬盘下载音轨的 Android 应用程序连接到 Google 云端硬盘。我无法创建用于身份验证的/token/目录。

我已经拆分了创建 GoogleAuthorizationCodeFlow 的过程,以显示该过程在失败之前进行了多远。我还尝试预先创建/token/目录,但也失败了。

这是相关代码。因为我使用的是内部存储,所以 list 文件中没有请求任何权限。

    @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

发生了两件我不明白的奇怪事情。

  1. 在这行 build.setDataStoreFactory(new FileDataStoreFactory(tokenFolder));

  2. 失败
  3. tokenFolder.mkdirs(); 没有按预期创建目录,也没有抛出异常。

另外,我不明白类名 GoogleAuthorizationCodeFlow.Builder 的术语 - 这只是一个带有句号的类名,还是对内部类或其他类的引用?

最佳答案

我希望您最终能找到这个“创建/token/目录” 问题的答案。

如果情况更糟,您只需要访问您知道其 Google 云端硬盘路径的 MP3 文件...

仅将 XXXXX 替换为您的文件 ID。现在将下载 MP3 字节,因此要么读入 bytearray,要么在 MP3 解码器/播放器中用作 URL。

关于android - 无法为 Google 云端硬盘创建 token 目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55793520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com