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

I making an app using google drive to store files for user to their google drive I followed google developer guide in https://developers.google.com/drive/api/guides/about-sdk and when i press the button to upload my files i got the err in title this is我使用谷歌驱动器制作一个应用程序来将用户的文件存储到他们的谷歌驱动器我遵循谷歌开发者指南https://developers.google.com/drive/api/guides/about-sdk当我按下按钮上传我的文件我得到了标题错误这是

my code:我的代码:

 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();
    }

error错误

The Application Default Credentials are not available.应用程序默认凭据不可用。 They are available if running in Google Compute Engine.如果在 Google Compute Engine 中运行,它们就可用。 Otherwise - java android否则 - java android

To use使用

 GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()

You must have the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials.您必须定义环境变量GOOGLE_APPLICATION_CREDENTIALS指向定义凭据的文件。

Other wise you can do it like shown in the Google drive java quickstart否则,您可以按照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.

相关问题 Google Dialogflow:应用程序默认凭据不可用 - Google Dialogflow: The Application Default Credentials are not available 应用程序默认凭据不可用 - Application Default Credentials not available 无法验证 google recaptcha 企业。 收到错误:java.io.IOException:应用程序默认凭据不可用 - Unable to validate google recaptcha enterprise. getting error: java.io.IOException: The Application Default Credentials are not available Google Cloud Storage API无法在App Engine上获取应用程序默认凭据,以查找Compute Engine错误 - Google Cloud Storage API cannot get Application Default Credentials on App Engine looking for Compute Engine error 如何为Google Compute Engine设置GOOGLE_APPLICATION_CREDENTIALS? - How to set GOOGLE_APPLICATION_CREDENTIALS for Google Compute Engine? 应用程序默认凭据不适用于 mac Google Cloud Storage 中的环境变量设置 - The Application Default Credentials are not available with environment variable setup in mac Google Cloud Storage GAE 凭据在 App Engine 上不可用 - GAE credentials not available on App Engine GCP DialogFlow API 错误:“应用程序默认凭据不可用” - GCP DialogFlow API error: "The Application Default Credentials are not available" 请求的资源不可用。 - The requested resource is not available. 谷歌应用引擎。 堆栈驱动程序。 使用 Java 进行日志记录 - Google App Engine. Stackdriver. Logging with Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM