刚开始使用Google Apis.在我的Google Cloud Platform帐户中,我为域范围委派创建了一个服务帐户.我为此服务帐户保存了json格式的私钥文件.
在我的测试应用程序中,我正在创建一个GoogleCredential实例:
var credential =
GoogleCredential.FromStream(new FileStream("privatekey.json", FileMode.Open, FileAccess.Read))
.CreateScoped(Scopes);
Run Code Online (Sandbox Code Playgroud)
如何设置我想要冒充的用户?使用p12私钥时,我可以执行以下操作:
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer("xxx@developer.gserviceaccount.com") //service Account id
{
Scopes = Scopes,
User = "admin@xxx.com" //the user to be impersonated
}.FromCertificate(new X509Certificate2(@"xxx.p12", "notasecret", X509KeyStorageFlags.Exportable)));
Run Code Online (Sandbox Code Playgroud)
但是,我如何使用GoogleCredential和json privatkey文件"轻松实现"?
亲切的问候