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
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 = "*****",
});
{
"type": "service_account",
"project_id": "...",
"private_key_id": "....",
"private_key": "....",
"client_email": ".....@developer.gserviceaccount.com",
"client_id": "....",
"auth_uri": "...accounts.google.com/o/oauth2/auth",
"token_uri": "...accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "...www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "...www.googleapis.com/robot/v1/metadata/x509/....-compute%40developer.gserviceaccount.com"
}
var _pathJson = @"C:\servicekey.json";
var json = File.ReadAllText(_pathJson);
var cr = JsonConvert.DeserializeObject<PersonalServiceAccountCred>(json);
// "personal" service account credential
// Create an explicit ServiceAccountCredential credential
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(cr.ClientEmail)
{
Scopes = new[] { YouTubeService.Scope.YoutubeUpload /*Here put scope that you want use*/}
}.FromPrivateKey(cr.PrivateKey));