Google云存储:来电者没有权限

时间:2016-10-19 04:20:54

标签: java rotation google-cloud-storage service-accounts

我正在尝试使用服务帐户和API旋转密钥以访问GCS存储桶。我已将所有角色启用到我的服务帐户,但仍然收到如下错误。

{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "The caller does not have permission",
    "reason" : "forbidden"
  } ],
  "message" : "The caller does not have permission",
  "status" : "PERMISSION_DENIED"
}

这是我的代码:

public static void main(String[] args) {

        HttpTransport transport;
        try {
            transport = GoogleNetHttpTransport.newTrustedTransport();

        JsonFactory jsonFactory = new JacksonFactory();

        Iam iam = new Iam(transport,jsonFactory,new HttpRequestInitializer() {
            public void initialize(HttpRequest httpRequest) {
                httpRequest.setConnectTimeout(0);
                httpRequest.setReadTimeout(0);
            }
        });

        CreateServiceAccountKeyRequest createServiceAccountKeyRequest = new CreateServiceAccountKeyRequest();

        Create create = iam.projects().serviceAccounts().keys().create("projects/mysampleproject/serviceAccounts/myserviceaccount@newsampleproject-123465.iam.gserviceaccount.com", createServiceAccountKeyRequest);
        create.setKey("AIzaSyC_YlBg_UXEFgdsspbGLvyb-THrTCbbZA");
        ServiceAccountKey serviceAccountKey =create.execute();
        System.out.println(serviceAccountKey.getPrivateKeyData());
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

我尝试了同样的post。请任何人建议我在哪里错了或如何以正确的方式实现它?

1 个答案:

答案 0 :(得分:0)

“mysampleproject”需要匹配“newsampleproject-123465.iam.gserviceaccount.com”中的“newsampleproject-123465”

尝试使用Google Application Default Credentials

也是值得的
     HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();    
     JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

     // Authenticate using Google Application Default Credentials.
     GoogleCredential c= GoogleCredential.getApplicationDefault();

     Iam iam = new Iam(httpTransport, jsonFactory, credential);