0

Need to authenticate google service account with OAuth. I have google credentials file in json format. refereed Google Service Account Authentication with Json file

able to create RASParameters but didn't know how to proceed further. I am very new to google apis implementation. found various examples of .p12 file but didn't find one with .json file. And i am using .net 4.0 framework so can't use the googlecredential class which is available from .net 4.5+ framework.

anuja
  • 3
  • 3

1 Answers1

1

I think that what you need to create is a JsonCredentialParameters with that Json file you have. This class is under the Google.Apis.Auth.OAuth2 namespace, so you just need to have the proper Google Apis Auth package to do this.

Then you can create a new ServiceAccountCredential with the proper user account and scopes.

protected override ServiceAccountCredential GetCredential()
    {
        var credentialParameters = GetCredentialParameters();
        return new ServiceAccountCredential(
           new ServiceAccountCredential.Initializer(credentialParameters.ClientEmail)
           {
               Scopes = Scopes,
               User = User
           }.FromPrivateKey(credentialParameters.PrivateKey));
    }
    JsonCredentialParameters GetCredentialParameters()
    {
        return JsonSerializer.Deserialize<JsonCredentialParameters>(new FileStream(File, FileMode.Open));
    }
marcofo88
  • 375
  • 2
  • 10
  • I have tried installing Google.Apis.Auth.OAuth2 package but it is not working with .net 4.0. So I have installed Google.Apis.Authentication.OAuth2 – anuja Jan 24 '18 at 13:03