com.google.auth.oauth2.GoogleCredentials.createScoped(GoogleCredentials.java:222)处的StackOverflowError

时间:2020-03-01 10:54:31

标签: java google-cloud-platform google-photos-api

下面的代码在执行时引发此错误:

Caused by: java.lang.StackOverflowError
    at com.google.auth.oauth2.GoogleCredentials.createScoped(GoogleCredentials.java:222)

此代码中可能有什么问题

public static void uploadToGooglePhotos(File uploadFile) throws Exception {
    try {
        InputStream stream = new ByteArrayInputStream(GoogleJsonKey.JSON_KEY.getBytes(StandardCharsets.UTF_8));
        GoogleCredentials credentials = GoogleCredentials.fromStream(stream)
                .createScoped("https://www.googleapis.com/auth/cloud-platform");
        credentials.refreshIfExpired();
        PhotosLibrarySettings settings =
                PhotosLibrarySettings.newBuilder()
                        .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
                        .build();
        PhotosLibraryClient photosLibraryClient =
                PhotosLibraryClient.initialize(settings);
        UploadMediaItemRequest uploadRequest =
                UploadMediaItemRequest.newBuilder()
                        .setFileName(uploadFile.getName())
                        .setDataFile(new RandomAccessFile(uploadFile.getAbsolutePath(), "r"))
                        .build();
        UploadMediaItemResponse uploadResponse = photosLibraryClient.uploadMediaItem(uploadRequest);
        if (uploadResponse.getError().isPresent()) {
            UploadMediaItemResponse.Error error = uploadResponse.getError().get();
        } else {
            String uploadToken = uploadResponse.getUploadToken().get();
        }
    } catch (ApiException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

1 个答案:

答案 0 :(得分:1)

您可以尝试以下方法吗?

GoogleCredentials credentials = GoogleCredentials.fromStream(stream).createScoped(Collections.singleton("https://www.googleapis.com/auth/cloud-platform"))
相关问题