Google cloud platform 我如何授予云功能访问权限,使其能够访问G-suite用户的G-mail帐户

Google cloud platform 我如何授予云功能访问权限,使其能够访问G-suite用户的G-mail帐户,google-cloud-platform,google-cloud-functions,gmail-api,service-accounts,Google Cloud Platform,Google Cloud Functions,Gmail Api,Service Accounts,我想做什么。 我试图让云函数能够访问G-suite用户的G-mail帐户,这样我就可以在他们身上调用Gmail API监视函数 问题 无法使用云函数中具有委托权限的服务帐户对G-suite用户帐户调用函数。返回400错误,表示请求错误,前置条件失败 我尝试过的。 我已经在GCP中创建了一个服务帐户,通过遵循这一点,然后在G-suite中创建,为服务帐户提供 我已经测试了在我的电脑上直接运行下面代码访问G-suite用户的gmail帐户是否成功。我会注意到GoogleCredential已被弃用,

我想做什么。

我试图让云函数能够访问G-suite用户的G-mail帐户,这样我就可以在他们身上调用Gmail API监视函数

问题

无法使用云函数中具有委托权限的服务帐户对G-suite用户帐户调用函数。返回400错误,表示请求错误,前置条件失败

我尝试过的。

我已经在GCP中创建了一个服务帐户,通过遵循这一点,然后在G-suite中创建,为服务帐户提供

我已经测试了在我的电脑上直接运行下面代码访问G-suite用户的gmail帐户是否成功。我会注意到GoogleCredential已被弃用,所以我不会在我的云功能中使用它

public class WatchTest {

@Test
public void callWatchOnGSuiteUsers() throws IOException {
    String SERVICE_ACCOUNT_FILE_PATH = "/path/file.json";

    FileInputStream credentialsFile = new FileInputStream(SERVICE_ACCOUNT_FILE_PATH);
    GoogleCredential init = GoogleCredential.fromStream(credentialsFile);

    HttpTransport httpTransport = init.getTransport();
    JsonFactory jsonFactory = init.getJsonFactory();

    GoogleCredential credentials = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
            .setServiceAccountId(init.getServiceAccountId())
            .setServiceAccountPrivateKey(init.getServiceAccountPrivateKey())
            .setServiceAccountScopes(Collections.singleton(GmailScopes.MAIL_GOOGLE_COM))
            .setServiceAccountUser("user@example.com")
            .build();

    Gmail gmail = new Gmail.Builder(httpTransport, jsonFactory, credentials).setApplicationName("gmail").build();

    List<String> watchTopics = new ArrayList<>();
    watchTopics.add("INBOX");

    WatchRequest watchRequest = new WatchRequest();
    watchRequest.setLabelIds(watchTopics);
    watchRequest.setTopicName("projects/{project-id}}/topics/{topic-name}}");

    gmail.users().watch("user@example.com", watchRequest).execute();
}
很难理解服务帐户、云函数和Gmail API的协同工作方式,所以如果有人能在这方面帮助我,那就太棒了

谢谢

编辑1-G-suite控制台,显示对服务帐户的访问权限。

您是否有权访问gsuite控制台中的服务帐户?您好@guillaumeblaquiere,感谢您的回复:),我添加了一个图像(编辑1),显示我在G-suite中所做的操作,以便访问服务帐户。这就是你所说的授权访问吗?我已经仔细检查了客户端ID,它与我在GCP中的服务帐户屏幕中的ID相匹配。您是否有权在gsuite控制台中访问服务帐户?您好@guillaumeblaquiere,感谢您回复:),我添加了一个图像(编辑1),显示我在G-suite中所做的操作,以便访问服务帐户。这就是你所说的授权访问吗?我仔细检查了客户ID,它与我在GCP中的服务帐户屏幕中的ID相匹配。
gcloud functions deploy my-cloud-function 
--entry-point WatchTrigger
--service-account deletgated-authority-test@<project-id>.iam.gserviceaccount.com
--runtime java11
--region <example-region>
--trigger-resource <bucket>
--trigger-event google.storage.object.finalize
public class WatchTrigger implements BackgroundFunction<GcsEvent> {

@Override
public void accept(GcsEvent payload, Context context) throws Exception {
    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();

    List<String> scopes = new ArrayList<>();
    scopes.add(GmailScopes.MAIL_GOOGLE_COM);

    GoogleCredentials credentials = ServiceAccountCredentials.getApplicationDefault()
            .createScoped(scopes)
            .createDelegated("user@example.com")

    HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);

    Gmail gmail = new Gmail.Builder(httpTransport, jsonFactory, requestInitializer)
            .setApplicationName("GMAIL")
            .build();

    ListLabelsResponse listResponse = gmail.users().labels().list("user@example.com").execute();

    List<Label> labels = listResponse.getLabels();
    if (labels.isEmpty()) {
        System.out.println("No labels found.");
    } else {
        System.out.println("Labels:");
        for (Label label : labels) {
            System.out.printf("- %s\n", label.getName());
        }
    }
}
com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
GET https://www.googleapis.com/gmail/v1/users/user@example.com/labels
{
"code" : 400,
"error" : [ {
"domain" : "global",
"message" : Precondition check failed.".
"reason" : "failedPrecondition"
} ]