Caused by: com.google.api.client.http.HttpResponseException: 400 Bad Request POST https://oauth2.googleapis.com/token

huangapple 未分类评论46阅读模式
英文:

Caused by: com.google.api.client.http.HttpResponseException: 400 Bad Request POST https://oauth2.googleapis.com/token

问题

以下是翻译好的代码部分:

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;
}
英文:

I'm trying to get bucket from GCP. I'm getting this problem. error“:”invalid_scope“,”error_description“:”Invalid OAuth scope or ID token audience provided."

public String uploadImage(String fileName , String filePath,String fileType) throws IOException {
   Bucket bucket = getBucket(&quot;serviceRequests&quot;);
    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&lt;String&gt; ar=new ArrayList&lt;String&gt;();
	  ar.add(&quot;https://storage.googleapis.com/saffrontest&quot;);
	  
     GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(&quot;/home/sparity/Downloads/Inhabitr Apps-3009206a82c0.json&quot;)).createScoped(ar);
    Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
   // Storage storage = StorageOptions.newBuilder().setHost(&quot;https://storage.googleapis.com/saffrontest&quot;).build().getService();
    Bucket bucket = storage.get(bucketName);
    if (bucket == null) {
      throw new IOException(&quot;Bucket not found:&quot;+bucketName);
    }
    return bucket;
  }

答案1

得分: 0

根据错误描述,处理 GoogleCredentials(https://developers.google.com/identity/protocols/oauth2/service-account)时未检测到任何范围或无效的范围。由于您明确尝试在代码中传递服务账号文件,我建议查阅云存储的范围 URL(https://cloud.google.com/storage/docs/authentication#oauth-scopes)。

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

英文:

As the error description suggests, no scopes or invalid scopes were detected while processing the GoogleCredentials (https://developers.google.com/identity/protocols/oauth2/service-account).
Since you are explicitly trying to pass your service account file in code, I would suggest going through the scope URLs for Cloud Storage (https://cloud.google.com/storage/docs/authentication#oauth-scopes).

It seems that 'https://www.googleapis.com/auth/cloud-platform' would work instead of 'https://storage.googleapis.com/saffrontest'. For a working exaple please refer to the last link (https://cloud.google.com/docs/authentication/production#passing_code
)

huangapple
  • 本文由 发表于 2020年7月26日 13:43:34
  • 转载请务必保留本文链接:https://java.coder-hub.com/63096523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定