error: no suitable method found for newArrayList(String)

error: no suitable method found for newArrayList(String)

我是 Java 的新手,正在尝试使用 Google 云服务。当我尝试按照本指南使用明确的方式指向我的服务帐户时; https://cloud.google.com/docs/authentication/production#passing_code

import com.google.api.client.util.Lists;
import com.google.api.gax.paging.Page;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.io.FileInputStream;
import java.io.IOException;



static void authExplicit(String jsonPath) throws IOException {
    // You can specify a credential file by providing a path to GoogleCredentials.
    // Otherwise credentials are read from the GOOGLE_APPLICATION_CREDENTIALS environment variable.
    GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
            .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
    Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

    System.out.println("Buckets:");
    Page<Bucket> buckets = storage.list();
    for (Bucket bucket : buckets.iterateAll()) {
        System.out.println(bucket.toString());
    }
}

我已经安装了要求。但是,这段代码会抛出如下错误;

error: no suitable method found for newArrayList(String)
    GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath)).createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
                                                                                                                  ^
method Lists.<E#1>newArrayList() is not applicable
  (cannot infer type-variable(s) E#1
    (actual and formal argument lists differ in length))
method Lists.<E#2>newArrayList(Iterable<? extends E#2>) is not applicable
  (cannot infer type-variable(s) E#2
    (argument mismatch; String cannot be converted to Iterable<? extends E#2>))
method Lists.<E#3>newArrayList(Iterator<? extends E#3>) is not applicable
  (cannot infer type-variable(s) E#3

    (argument mismatch; String cannot be converted to Iterator<? extends E#3>)) where E#1,E#2,E#3 are type-variables:

E#1 extends Object declared in method <E#1>newArrayList()
E#2 extends Object declared in method <E#2>newArrayList(Iterable<? extends E#2>)
E#3 extends Object declared in method <E#3>newArrayList(Iterator<? extends E#3>)

我不确定我的设置有什么问题,也没有找到答案。

所以快速浏览一下 google 的文档 https://cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.Lists, it doesn't contain a method newArrayList that takes a String as a parameter, but google has another library (guava) that contains the method you are looking for, https://guava.dev/releases/19.0/api/docs/com/google/common/collect/Lists.html,然后用 import com.google.common.collect.Lists;;

替换你的 import com.google.api.client.util.Lists