小编Ren*_*Dev的帖子

由json私钥文件(ServiceAccount)创建的GoogleCredential - 如何设置用户进行模拟?

刚开始使用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文件"轻松实现"?

亲切的问候

.net c# google-api google-api-dotnet-client

7
推荐指数
2
解决办法
4682
查看次数

标签 统计

.net ×1

c# ×1

google-api ×1

google-api-dotnet-client ×1