我正在实施一个Android应用程序,用户订阅指定的youtube频道。我正在使用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.Please指导我如何解决此异常
答案 0 :(得分:1)
我将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");
...