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;
}
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 )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.