how use Google OAuth2 using ServiceAccount in .net?

前端 未结 2 1241
谎友^
谎友^ 2020-12-11 17:02

Is there any sample how to access a google service API using service account in .net?

private const string SERVICE_ACCOUNT_EMAIL = \"xxxxxxxxxxx@developer.gs         


        
2条回答
  •  南笙
    南笙 (楼主)
    2020-12-11 17:42

    This case work in my site

    var certificate = new X509Certificate2("pathTo***.p12", "notasecret", X509KeyStorageFlags.Exportable);
            var serviceAccountEmail = "********-*********@developer.gserviceaccount.com";
            var userAccountEmail = "******@gmail.com";
            ServiceAccountCredential credential = new ServiceAccountCredential(
                       new ServiceAccountCredential.Initializer(serviceAccountEmail)
                       {
                           Scopes = new[] { DriveService.Scope.Drive },
                           User = userAccountEmail
    
                       }.FromCertificate(certificate));
    
            // Create the service.
            var service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "*****",
            });
    

提交回复
热议问题