[英]Authenticating to Google API with OAuth2
我正在嘗試使用C#/ .NET 執行Google Apps域范圍授權的示例代碼,並且像我嘗試的其他一些示例一樣,創建使用auth變量的對象的代碼部分表示他們語法錯了。 這是我的代碼:
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Drive.v2;
using Google.Apis.Util;
namespace GoogleAPIDemo
{
class DriveServiceObject
{
private const string SERVICE_ACCOUNT_EMAIL = "<some-id>@developer.gserviceaccount.com";
private const string SERVICE_ACCOUNT_PKCS12_FILE_PATH = @"\path\to\<public_key_fingerprint>-privatekey.p12";
/// <summary>
/// Build a Drive service object authorized with the service account
/// that acts on behalf of the given user.
/// </summary>
/// @param userEmail The email of the user.
/// <returns>Drive service object.</returns>
static DriveService BuildService(String userEmail)
{
X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret",
X509KeyStorageFlags.Exportable);
var provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, certificate)
{
ServiceAccountId = SERVICE_ACCOUNT_EMAIL,
Scope = DriveService.Scopes.Drive.GetStringValue(),
ServiceAccountUser = userEmail,
};
var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);
return new DriveService(auth);
}
}
}
我看到的錯誤是
(local variable) OAuth2Athenticator<AssertionFlowClient> auth
Error:
The best overloaded method match for 'Google.Apis.DriveService.DriveService(Googel.Apis.Services.BaseClientService.Initializer)' has some invalid arguments
這是我第一次編寫使用Google API的應用程序,任何幫助實現這項工作將不勝感激!
這個工作:
var provider = new AssertionFlowClient(
GoogleAuthenticationServer.Description,
new X509Certificate2(privateKeyPath, keyPassword, X509KeyStorageFlags.Exportable))
{
ServiceAccountId = serviceAccountEmail,
Scope = DriveService.Scopes.Drive.GetStringValue(),
ServiceAccountUser = driveHolderAccountEmail
};
var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);
m_service = new DriveService(new BaseClientService.Initializer()
{
Authenticator = auth
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.