Google云端存储JSON API服务帐户403禁止访问

时间:2016-11-22 13:38:19

标签: java google-cloud-storage json-api

我正在尝试使用Java JSON API访问Google Cloud Storage存储桶对象。

我们使用服务帐户身份验证。我们还检查我们对服务帐户ID的许可为"编辑"。

当我们尝试列出存储桶对象时,我们得到了以下错误。

403 Forbidden { "code" : 403, "errors" : [ { "domain" : "global", "message" : "Forbidden", "reason" : "forbidden" } ], "message" : "Forbidden" }

验证码

HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = new JacksonFactory();

GoogleCredential credential = null;
InputStream credentialsStream = null;
try {
  credentialsStream = new FileInputStream("<Location of Service Account JSON File>");
  credential =GoogleCredential.fromStream(credentialsStream, transport, jsonFactory);
} catch (IOException e) {

} finally {
  if (credentialsStream != null) {
    credentialsStream.close();
  }
}           


 if (credential.createScopedRequired()) {
      Collection<String> scopes = StorageScopes.all();
      credential = credential.createScoped(scopes);
    }
return new Storage.Builder(transport, jsonFactory, credential)
    .setApplicationName("DemoApplication")
    .build();

Bucket Object Code

Storage client = StorageFactory.getService();

Storage.Objects.List listRequest = client.objects().list(bucketName);

List<StorageObject> results = new ArrayList<StorageObject>();
Objects objects;

// Iterate through each page of results, and add them to our results list.
do {
  objects = listRequest.execute();
  // Add the items in this page of results to the list we'll return.
  results.addAll(objects.getItems());

  // Get the next page, in the next iteration of this loop.
  listRequest.setPageToken(objects.getNextPageToken());
} while (null != objects.getNextPageToken());

请帮我解决这个问题。

0 个答案:

没有答案