【问题标题】:StackOverflowError at com.google.auth.oauth2.GoogleCredentials.createScoped(GoogleCredentials.java:222)StackOverflowError 在 com.google.auth.oauth2.GoogleCredentials.createScoped(GoogleCredentials.java:222)
【发布时间】:2020-03-01 10:54:31
【问题描述】:

下面的代码在执行时会抛出这个错误:

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

}

【问题讨论】:

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


    【解决方案1】:

    您可以尝试以下方法吗:

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

    【讨论】:

      猜你喜欢
      • 2021-11-06
      • 2016-05-26
      • 2013-08-09
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多