gpt4 book ai didi

java - 无法创建/.oauth-credentials 目录

转载 作者:行者123 更新时间:2023-11-30 01:43:49 25 4
gpt4 key购买 nike

我正在实现一个 Android 应用程序,用户可以在其中订阅分配的 youtube channel 。我正在使用 google api 客户端库。我从开发人员控制台获得了客户端 json 文件。

我使用了来自 google api 示例的代码,例如添加 subsription.java 文件和 auth.java 用于授权和存储 o-auth 凭据

 private static final String CREDENTIALS_DIRECTORY = ".oauth-credentials";
public static Credential authorize(List<String> scopes, String credentialDatastore) throws IOException {

// Load client secrets.
Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("/client_secrets.json"));
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, clientSecretReader);

// Checks that the defaults have been replaced (Default = "Enter X here").

// This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore}
FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY));
DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore(credentialDatastore);

我收到无法创建目录的 IO 异常 ./o-authcredentials。请指导我如何解决此异常

最佳答案

我将我的 MainActivity 作为参数传递给函数 authorize 以便能够在不使用代码 System.getProperty("user. home"),它总是返回空字符串。所以我得到了:

import android.util.Log;

public class Auth {
private static final String CREDENTIALS_DIRECTORY = ".oauth-credentials";
private final static String TAG = Auth.class.getSimpleName();

public static Credential authorize(List<String> scopes, String credentialDatastore, MainActivity mainActivity) throws Exception {
...
String dataDir = mainActivity.getApplicationInfo().dataDir;
String storePath = dataDir + File.separator + CREDENTIALS_DIRECTORY;
Log.d(TAG, "storePath: " + storePath);

File file=new File(storePath);
FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(file);
Log.d(TAG, "fileDataStoreFactory successfully created");

DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore(credentialDatastore);
Log.d(TAG, "datastore successfully retrieved if existed or created if not existed yet");
...

关于java - 无法创建/.oauth-credentials 目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34018036/

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