【发布时间】: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