question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow setting User on ServiceAccountCredential when using GoogleCredential

See original GitHub issue
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 = "user@example.com";
    initializer.Scopes = new List<string>
    {
        DirectoryService.Scope.AdminDirectoryUserReadonly
    };

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12

github_iconTop GitHub Comments

1reaction
chrisdunelmcommented, Jul 14, 2017

Yes, exactly. I expect we’ll release a new version to nuget with this fix sometime next week.

0reactions
kspearrincommented, Jul 14, 2017

@chrisdunelm Looks like that will work. I assume the syntax for scopes and user would be?

GoogleCredential.FromStream(stream).CreateScoped(scopes).CreateWithUser(user)
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Creating a ServiceAccountCredential for a user from ...
Here's how I'd do it, setting both the scopes and the new user at the same time via the ServiceAccountCredential.Initializer :
Read more >
Using OAuth 2.0 for Server to Server Applications
The code above uses the GoogleCredential object to call its createDelegated() method. The argument for the createDelegated() method must be a user which...
Read more >
Class GoogleCredential (1.60.0) | .NET client library
The simplest way to get a credential for this purpose is to create a service account using the Google Developers Console in the...
Read more >
Create and delete service account keys | IAM Documentation
How to create and delete service account keys.
Read more >
Using OAuth 2.0 with the Google API Client Library for Java
Purpose: This document explains how to use the GoogleCredential utility class to do OAuth 2.0 authorization with Google services.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found