google activity api服务帐户403

时间:2015-11-17 13:57:00

标签: c# google-api google-drive-api google-api-dotnet-client service-accounts

我正在使用服务帐户访问我们的域用户的Google云端硬盘,这就像一个魅力!但是,我现在正尝试使用相同的服务帐户来获取用户在Google云端硬盘中的活动。

我已授予对https://www.googleapis.com/auth/activityhttps://www.googleapis.com/auth/drive

的服务帐户API访问权限

并启用API!

但是我总是得到错误403,禁止!

static AppsactivityService BuildService(String userEmail)
    {
        X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH,
            "notasecret", X509KeyStorageFlags.Exportable);
        ServiceAccountCredential credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(SERVICE_ACCOUNT_EMAIL)
            {
                Scopes = new[] { AppsactivityService.Scope.Drive },
                User = userEmail
            }.FromCertificate(certificate));

        // Create the service.
        var service = new AppsactivityService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "mytest app",
        });

        return service;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        var service = BuildService("someuser@domain.com");

        ActivitiesResource.ListRequest listRequest = service.Activities.List();
        listRequest.Source = "drive.google.com";
        listRequest.DriveAncestorId = "root";

        listRequest.PageSize = 10;

        IList<Activity> activities = listRequest.Execute().Activities;

        Console.WriteLine("Activities:");
    }

可能需要一些时间来启用https://www.googleapis.com/auth/activity

1 个答案:

答案 0 :(得分:0)

找到答案......只是服务中的一个愚蠢错误

更改

AppsactivityService.Scope.Drive 
to 
AppsactivityService.Scope.Activity 

THX!