简体   繁体   中英

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

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. Otherwise - 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.

Other wise you can do it like shown in the Google drive java quickstart

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM