Allow setting User on ServiceAccountCredential when using GoogleCredential
See original GitHub issuevar 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:
- Created 6 years ago
- Comments:12
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, exactly. I expect we’ll release a new version to nuget with this fix sometime next week.
@chrisdunelm Looks like that will work. I assume the syntax for scopes and user would be?