C# 如何在.net中使用ServiceAccount使用Google OAuth2?
有没有关于如何使用.net中的服务帐户访问google服务API的示例C# 如何在.net中使用ServiceAccount使用Google OAuth2?,c#,google-api,google-oauth,C#,Google Api,Google Oauth,有没有关于如何使用.net中的服务帐户访问google服务API的示例 private const string SERVICE_ACCOUNT_EMAIL = "xxxxxxxxxxx@developer.gserviceaccount.com"; private const string SERVICE_ACCOUNT_PKCS12_FILE_PATH = @"\path\test-privatekey.p12"; static DriveService BuildService() {
private const string SERVICE_ACCOUNT_EMAIL = "xxxxxxxxxxx@developer.gserviceaccount.com";
private const string SERVICE_ACCOUNT_PKCS12_FILE_PATH = @"\path\test-privatekey.p12";
static DriveService BuildService()
{
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(),
};
var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);
return new DriveService((new BaseClientService.Initializer()
{
Authenticator = auth
});
}
private const string服务\u帐户\u电子邮件=”xxxxxxxxxxx@developer.gserviceaccount.com";
private const string SERVICE_ACCOUNT_PKCS12_FILE_PATH=@“\PATH\test privatekey.p12”;
静态驱动服务BuildService()
{
X509Certificate2 certificate=新的X509Certificate2(服务\帐户\ PKCS12\文件\路径,“notasecret”,
X509keystrageFlags.可出口);
var provider=new AssertionFlowClient(GoogleAuthenticationServer.Description,certificate)
{
ServiceAccountId=服务帐户电子邮件,
Scope=DriveService.Scopes.Drive.GetStringValue(),
};
var auth=新的OAuth2Authenticator(提供程序,AssertionFlowClient.GetState);
返回新的DriveService((新的BaseClientService.Initializer()
{
验证器=auth
});
}
这无法成功返回OAuth连接。如何才能做到这一点?此案例在我的网站中有效
var certificate = new X509Certificate2("pathTo***.p12", "notasecret", X509KeyStorageFlags.Exportable);
var serviceAccountEmail = "********-*********@developer.gserviceaccount.com";
var userAccountEmail = "******@gmail.com";
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { DriveService.Scope.Drive },
User = userAccountEmail
}.FromCertificate(certificate));
// Create the service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "*****",
});
var\u pathJson=@“C:\servicekey.json”;
var json=File.ReadAllText(_pathJson);
var cr=JsonConvert.DeserializeObject(json);
//“个人”服务帐户凭据
//创建显式ServiceAccountCredential凭据
var凭证=新ServiceAccountCredential(新ServiceAccountCredential.Initializer(cr.ClientEmail)
{
Scopes=new[]{YouTubeService.Scope.YoutubeUpload/*此处放置要使用的作用域*/}
}.来自PrivateKey(cr.PrivateKey));
你找到解决方案了吗?我遇到了一个奇怪的行为:每当我放置一个甚至是绝对路径时,我总是得到一个例外:“系统找不到指定的文件。| |路径:C:\www\myproject\App_Data\key.p12”,我将这个文件放在App_数据文件夹中,并将其标记为“始终复制”
{
"type": "service_account",
"project_id": "...",
"private_key_id": "....",
"private_key": "....",
"client_email": ".....@developer.gserviceaccount.com",
"client_id": "....",
"auth_uri": "...accounts.google.com/o/oauth2/auth",
"token_uri": "...accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "...www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "...www.googleapis.com/robot/v1/metadata/x509/....-compute%40developer.gserviceaccount.com"
}
var _pathJson = @"C:\servicekey.json";
var json = File.ReadAllText(_pathJson);
var cr = JsonConvert.DeserializeObject<PersonalServiceAccountCred>(json);
// "personal" service account credential
// Create an explicit ServiceAccountCredential credential
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(cr.ClientEmail)
{
Scopes = new[] { YouTubeService.Scope.YoutubeUpload /*Here put scope that you want use*/}
}.FromPrivateKey(cr.PrivateKey));