繁体   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