我需要将S preadsheetsService,但我没有找到办法在官方文档 .NET
。
的https://developers.google.com/google-apps/s$p$padsheets/?hl=ja#authorizing_requests
I need to use SpreadsheetsService, but I don't find the way in the official documentation .net
.
https://developers.google.com/google-apps/spreadsheets/?hl=ja#authorizing_requests
我有我的服务进行身份验证:
I have my service authenticated:
String serviceAccountEmail = "serviceAccount@developer.gserviceaccount.com";
var certificate = new X509Certificate2(@"privatekey.p12", "pass", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { DriveService.Scope.Drive }
}.FromCertificate(certificate));
在这里,我可以实例几乎任何服务。
From here I can instantiate almost any service.
例如驱动器服务:
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Drive API Sample",
});
但随着取值preadsheetsService
我能做到这一点,因为SS preadsheetsService等待一个字符串应用程序名在他的默认构造函数或在GOAuth2RequestFactory他的财产 RequestFactory
。
But with SpreadsheetsService
I can do this, because SpreadsheetsService wait for an string 'application name' in his default constructor or an GOAuth2RequestFactory in his property RequestFactory
.
如何与ServiceAccountCredential验证US preadsheetsService?
How to authenticate SpreadsheetsService with an ServiceAccountCredential?
推荐答案
下面是如何做到这一点的答案..... 关键是使用customHeaders上一个新requestFactory
Here is the answer on how to do this..... The key is to use the customHeaders on a new requestFactory
var certificate = new
System.Security.Cryptography.X509Certificates.X509Certificate2(p12KeyFileName,
"notasecret", X509KeyStorageFlags.Exportable);
string SCOPE = "https://spreadsheets.google.com/feeds
https://docs.google.com/feeds";
Google.Apis.Auth.OAuth2.ServiceAccountCredential credential = new
Google.Apis.Auth.OAuth2.ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { SCOPE }
}.FromCertificate(certificate));
bool success =
credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result;
var requestFactory = new Google.GData.Client.GDataRequestFactory("My
Plastic SCM Service");
requestFactory.CustomHeaders.Add(string.Format("Authorization: Bearer
{0}", credential.Token.AccessToken));
var s = new
SpreadsheetsService("MySpreadsheetIntegration-v1"); s.RequestFactory =
requestFactory;
return s;
相关推荐
最新文章