Skip to content

Allow setting User on ServiceAccountCredential when using GoogleCredential #1006

Closed
@kspearrin

Description

@kspearrin
var creds = GoogleCredential.FromStream(stream).CreateScoped(scopes);

When creating a GoogleCredential from a JSON key for a service account, the resulting UnderlyingCredential (ServiceAccountCredential) provides no way of setting the User property.

The code here looks like it is intending to copy a user over, but no user is ever set. It only derives from the JSON. Maybe we need a method like is provided for adding the scope (CreateScoped).

For now, the only way I can get this to work is to create my own ServiceAccountCredential instead of using GoogleCredential. Basically, I am just copy/pasting the code from the existing DefaultCredentialProvider and adding my User and Scopes.

...

var credentialParameters = NewtonsoftJsonSerializer.Instance.Deserialize<JsonCredentialParameters>(stream);

...

private static ServiceAccountCredential CreateServiceAccountCredentialFromParameters(
    JsonCredentialParameters credentialParameters)
{
    if(credentialParameters.Type != JsonCredentialParameters.ServiceAccountCredentialType ||
        string.IsNullOrEmpty(credentialParameters.ClientEmail) ||
        string.IsNullOrEmpty(credentialParameters.PrivateKey))
    {
        throw new InvalidOperationException("JSON data does not represent a valid service account credential.");
    }

    var initializer = new ServiceAccountCredential.Initializer(credentialParameters.ClientEmail);
    initializer.User = "[email protected]";
    initializer.Scopes = new List<string>
    {
        DirectoryService.Scope.AdminDirectoryUserReadonly
    };

    return new ServiceAccountCredential(initializer.FromPrivateKey(credentialParameters.PrivateKey));
}

Metadata

Metadata

Assignees

Labels

Type: Enhancementpriority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions