cha*_*ran 8 c# asp.net google-api google-oauth google-api-dotnet-client
我在使用asp.net c#的google api工作,我的目标是使用服务帐户访问google api.
我已导入所有需要的dll以创建服务帐户以访问管理员功能(admin sdk).
但我找不到ServiceAccountCredential.
我如何在我的项目中实现这一点?
Ufu*_*arı 17
以下是2017年的表现方式:
在您的代码中引用Google.Apis.Auth包
using (var stream = new FileStream("key.json", FileMode.Open, FileAccess.Read))
{
var credential = GoogleCredential.FromStream(stream)
.CreateScoped(scopes)
.UnderlyingCredential as ServiceAccountCredential;
//profit
}
Run Code Online (Sandbox Code Playgroud)DaI*_*mTo 10
ServiceAccountCredential是Google.Apis.Auth.OAuth2的一部分
使用BigQuery的简单示例:
using System;
using Google.Apis.Auth.OAuth2;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Bigquery.v2;
using Google.Apis.Services;
//Install-Package Google.Apis.Bigquery.v2
namespace GoogleBigQueryServiceAccount
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("BigQuery API - Service Account");
Console.WriteLine("==========================");
String serviceAccountEmail = "539621478854-imkdv94bgujcom228h3ea33kmkoefhil@developer.gserviceaccount.com";
var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { BigqueryService.Scope.DevstorageReadOnly }
}.FromCertificate(certificate));
// Create the service.
var service = new BigqueryService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "BigQuery API Sample",
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10716 次 |
| 最近记录: |