英文:
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("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;
}
答案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
)
专注分享java语言的经验与见解,让所有开发者获益!
评论