簡體   English   中英

應用程序默認憑據不可用。 如果在 Google Compute Engine 中運行,它們就可用。 否則 - java android

[英]The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise - java android

我使用谷歌驅動器制作一個應用程序來將用戶的文件存儲到他們的谷歌驅動器我遵循谷歌開發者指南https://developers.google.com/drive/api/guides/about-sdk當我按下按鈕上傳我的文件我得到了標題錯誤這是

我的代碼:

 try {
        // Load pre-authorized user credentials from the environment.
        GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
                .createScoped(Arrays.asList(DriveScopes.DRIVE_FILE));
        HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
                credentials);

        Drive service = new com.google.api.services.drive.Drive.Builder(new NetHttpTransport(),
                GsonFactory.getDefaultInstance(),
                requestInitializer)
                .setApplicationName(getString(R.string.app_name))
                .build();

        File fileMetaDate = new File();
        fileMetaDate.setName(getString(R.string.db_name_lists));
        File file1 = service.files().create(fileMetaDate, fileContent)
                .setFields("id")
                .execute();
        Toast.makeText(this, file1.getId() + " has been created", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printStackTrace();
    }

錯誤

應用程序默認憑據不可用。 如果在 Google Compute Engine 中運行,它們就可用。 否則 - java android

使用

 GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()

您必須定義環境變量GOOGLE_APPLICATION_CREDENTIALS指向定義憑據的文件。

否則,您可以按照Google 驅動器 java 快速入門中所示的方式進行操作

private static final String CREDENTIALS_FILE_PATH = "/credentials.json";

InputStream in = DriveQuickstart.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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM