6

Tôi cần sử dụng SpreadsheetsService, nhưng tôi không tìm cách trong tài liệu chính thức .net. https://developers.google.com/google-apps/spreadsheets/?hl=ja#authorizing_requestsCách sử dụng SpreadsheetsService được xác thực bởi ServiceAccountCredential?

tôi có dịch vụ của tôi được chứng thực:

String serviceAccountEmail = "[email protected]"; 

var certificate = new X509Certificate2(@"privatekey.p12", "pass", X509KeyStorageFlags.Exportable); 

ServiceAccountCredential credential = new ServiceAccountCredential(
    new ServiceAccountCredential.Initializer(serviceAccountEmail) 
    { 
     Scopes = new[] { DriveService.Scope.Drive } 
    }.FromCertificate(certificate)); 

Từ đây tôi có thể nhanh chóng hầu hết các dịch vụ.

Ví dụ Ổ dịch vụ:

var service = new DriveService(new BaseClientService.Initializer() 
    { 
      HttpClientInitializer = credential, 
      ApplicationName = "Drive API Sample", 
    }); 

Nhưng với SpreadsheetsService tôi có thể làm được điều này, bởi vì SpreadsheetsService chờ đợi cho một chuỗi 'tên ứng dụng' trong constructor mặc định của mình hoặc một GOAuth2RequestFactory trong tài sản RequestFactory mình.

Cách xác thực SpreadsheetsService với ServiceAccountCredential?

+1

Với Java bạn ca n sử dụng: spreadsheetService.setHeader ("Ủy quyền", "Bearer" + accessToken); Nhưng không chắc chắn về .net .... setHeader ví dụ: http://stackoverflow.com/questions/15084133/issue-with-oauth2-authentication-with-google-spreadsheet/15152682#15152682 – eddyparkinson

+0

Bạn có tìm thấy câu trả lời không? Tôi đang cố gắng làm điều tương tự, nhưng SpreadsheetsService chỉ hỗ trợ tên ứng dụng làm tham số. Có cách nào bạn sử dụng không? setUserCredentials hoạt động đối với tôi nếu tôi chỉ là người dùng thường xuyên chứ không phải tên miền. – Sergei

+0

__Địa điểm và lắp ráp_ cho 'DriveService' là gì? –

Trả lời

5

Dưới đây là câu trả lời về cách để làm điều này ..... Điều quan trọng là sử dụng customHeaders trên requestFactory mới

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; 
0

Lưu ý rằng khi thêm tiêu đề tùy chỉnh bạn cần: "Authorization : Bearer {0} "< - có khoảng trắng giữa" Bearer "và" {0} " Vì: " Ủy quyền: Bearer {0} "< - không có không gian sẽ chỉ giúp bạn" tiêu đề ủy quyền không xác định " Lỗi 401 phản hồi

Các vấn đề liên quan