【问题标题】:Caused by: com.google.api.client.http.HttpResponseException: 400 Bad Request POST https://oauth2.googleapis.com/token引起:com.google.api.client.http.HttpResponseException: 400 Bad Request POST https://oauth2.googleapis.com/token
【发布时间】:2020-07-26 05:43:34
【问题描述】:

我正在尝试从 GCP 获取存储桶。我遇到了这个问题。 error“:”invalid_scope“,”error_description“:”提供的 OAuth 范围或 ID 令牌受众无效。”

public String uploadImage(String fileName , String filePath,String fileType) throws IOException {
   Bucket bucket = getBucket("serviceRequests");
    InputStream inputStream = new FileInputStream(new File(filePath));
    Blob blob = bucket.create(fileName, inputStream, fileType);
    return blob.getMediaLink();
  }

  private Bucket getBucket(String bucketName) throws IOException {
      Collection<String> ar=new ArrayList<String>();
      ar.add("https://storage.googleapis.com/saffrontest");
      
     GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("/home/sparity/Downloads/Inhabitr Apps-3009206a82c0.json")).createScoped(ar);
    Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
   // Storage storage = StorageOptions.newBuilder().setHost("https://storage.googleapis.com/saffrontest").build().getService();
    Bucket bucket = storage.get(bucketName);
    if (bucket == null) {
      throw new IOException("Bucket not found:"+bucketName);
    }
    return bucket;
  }

【问题讨论】:

    标签: java google-cloud-platform


    【解决方案1】:

    正如错误描述所暗示的,在处理 GoogleCredentials (https://developers.google.com/identity/protocols/oauth2/service-account) 时未检测到范围或无效范围。 由于您明确尝试在代码中传递您的服务帐户文件,我建议您通过 Cloud Storage (https://cloud.google.com/storage/docs/authentication#oauth-scopes) 的范围 URL。

    似乎“https://www.googleapis.com/auth/cloud-platform”可以代替“https://storage.googleapis.com/saffrontest”。有关工作示例,请参阅最后一个链接(https://cloud.google.com/docs/authentication/production#passing_code )

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 2017-11-30
      相关资源
      最近更新 更多