[英]where can i find ServiceAccountCredential
我正在使用 asp.net c# 在 google api 中工作,我的目標是使用服務帳戶訪問 google api。
我已經導入了所有需要的 dll 來創建服務帳戶來訪問管理功能(管理 sdk)。
但我找不到 ServiceAccountCredential。
我如何在我的項目中實現這一點?
以下是您在 2017 年的做法:
轉到服務帳戶頁面
為服務帳號創建一個私鑰作為 JSON 文件
將下載的文件放在您的項目中(用於開發)或在您的構建過程中烘焙它
在您的代碼中引用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 }
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",
});
}
}
}
我正在使用 Xamarin Studio,並且有一個 NUnit 測試庫項目 (PCL) 使用 ServiceAccountCredential 在我的 Mac 上運行。 我只是嘗試將其移動到Android測試項目(MonoDroid),並且ServiceAccountCredential不存在。 ServiceAccount 確實存在(它的祖先類)。 所以這個問題可能與編譯目標有關,並且沒有為您的目標實現 ServiceAccountCredential。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.