问题描述
刚开始使用Google Apis。
在我的Google云端平台帐户中,我为域范围委派创建了一个服务帐户。我为此服务帐户保存了json格式的私钥文件。
在我的测试应用程序中,我创建了一个GoogleCredential实例:
var credential =
GoogleCredential.FromStream(新的FileStream(privatekey.json,FileMode.Open,FileAccess.Read))
.CreateScoped(Scopes);
如何设置用户我要模拟?
当使用p12私钥时,我可以执行以下操作:
var credential = new ServiceAccountCredential(
新的ServiceAccountCredential.Initializer([email protected])//服务帐户ID
{
范围=范围,
用户[email protected]//用户假新
} .FromCertificate(新X509Certificate2(@xxx.p12,notasecret,X509KeyStorageFlags.Exportable)));
但是我怎么能用GoogleCredential和json privatkey文件简单的方法呢?
亲切的问候
好吧,我现在通过复制代码GoogleCredential和内部类DefaultCredentialProvider的内部使用(var fs = new FileStream(key.json,FileMode.Open, FileAccess.Read))
{
var credentialParameters =
NewtonsoftJsonSerializer.Instance.Deserialize< JsonCredentialParameters>(fs);
if(credentialParameters.Type!=service_account
|| string.IsNullOrEmpty(credentialParameters.ClientEmail)
|| string.IsNullOrEmpty(credentialParameters.PrivateKey))
抛出新的InvalidOperationException (JSON数据不代表有效的服务帐户凭证。);
返回新的ServiceAccountCredential(
新的ServiceAccountCredential.Initializer(credentialParameters.ClientEmail)
{
Scopes =范围,
User = _adminUser //要模拟的用户
} .FromPrivateKey(credentialParameters.PrivateKey));
$ / code>
如果有人(也许)有一个更好的主意,可以直接通过GoogleCredential直接给我提示;)
just starting with Google Apis.In my Google Cloud Platform account i created a Service Account for domain wide delegation. I saved a private key file in json format for this service account.
In my test application i am creating a GoogleCredential instance:
var credential =
GoogleCredential.FromStream(new FileStream("privatekey.json", FileMode.Open, FileAccess.Read))
.CreateScoped(Scopes);
How can i set the user i want to impersonate?When using a p12 private key i could do the following:
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer("[email protected]") //service Account id
{
Scopes = Scopes,
User = "[email protected]" //the user to be impersonated
}.FromCertificate(new X509Certificate2(@"xxx.p12", "notasecret", X509KeyStorageFlags.Exportable)));
But how can i do this "the easy way" with GoogleCredential and a json privatkey file?
Kind regards
Ok i solved it now by copying code from the insides of GoogleCredential and the internal class DefaultCredentialProvider
using (var fs = new FileStream("key.json", FileMode.Open, FileAccess.Read))
{
var credentialParameters =
NewtonsoftJsonSerializer.Instance.Deserialize<JsonCredentialParameters>(fs);
if (credentialParameters.Type != "service_account"
|| string.IsNullOrEmpty(credentialParameters.ClientEmail)
|| string.IsNullOrEmpty(credentialParameters.PrivateKey))
throw new InvalidOperationException("JSON data does not represent a valid service account credential.");
return new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(credentialParameters.ClientEmail)
{
Scopes = Scopes,
User = _adminUser //the user to be impersonated
}.FromPrivateKey(credentialParameters.PrivateKey));
}
If someone (maybe peleyal) has a better idea to do it directly via GoogleCredential feel free to give me a hint ;)
这篇关于由json私钥文件(ServiceAccount)创建的GoogleCredential - 如何设置用户模拟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!