Authenticate service account without downloaded key on google app engine

后端 未结 1 1376
盖世英雄少女心
盖世英雄少女心 2020-12-11 13:09

I am working on a product that is supposed to be installed in Google App Engine.

In this I am using Service account for authenticating Gmail API, Drive API, Calendar

相关标签:
1条回答
  • 2020-12-11 13:31

    I could achieve it by IAM API https://cloud.google.com/iam/reference/rest/v1/projects.serviceAccounts.keys

    Below is Java code for it

    AppIdentityCredential credential = new AppIdentityCredential(
                    Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
    Iam iam = new Iam(httpTRANSPORT, jsonFACTORY, credential);
    try {
        Iam.Projects.ServiceAccounts.Keys.Create keyCreate = iam.projects().serviceAccounts().keys()
                        .create("projects/myProject/serviceAccounts/myProject@appspot.gserviceaccount.com", new CreateServiceAccountKeyRequest());
    
        ServiceAccountKey key = keyCreate.execute();
    
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    

    Any key can be used to generate GoogleCredential as below

    InputStream stream = new ByteArrayInputStream(key.decodePrivateKeyData());
    GoogleCredential credential = GoogleCredential.fromStream(stream);
    
    0 讨论(0)
提交回复
热议问题