如何使用C#中的服务帐户从gmail访问委派的电子邮件 [英] How to access delegated email from gmail using service account in C#
本文介绍了如何使用C#中的服务帐户从gmail访问委派的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
如何使用服务帐户访问Gmail中的委托电子邮件?有没有例子?
我尝试过:
我试过但是我收到了一个错误,比如
Hi,
How can I access delegated email in Gmail using Service Account? Is there is any examples?
What I have tried:
I tried but I got an error like
Delegation denied for [403]
var service = new GmailService(); //change
using (var fs = new FileStream("xxxxxx.json", FileMode.Open, FileAccess.Read))
{
try
{
var credentialParameters =
NewtonsoftJsonSerializer.Instance.Deserialize<JsonCredentialParameters>(fs);
if (credentialParameters.Type != "service_account"
|| string.IsNullOrEmpty(credentialParameters.ClientEmail)
|| string.IsNullOrEmpty(credentialParameters.PrivateKey))
throw new InvalidOperationException("JSON data does not represent a valid service account credential.");
ServiceAccountCredential credential1 = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(credentialParameters.ClientEmail)
{
Scopes = Scopes,
User = "xxxxx.xxx.com" //the user to be impersonated
}.FromPrivateKey(credentialParameters.PrivateKey));
service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential1,
ApplicationName = "xxxxxxxx",
});
}
catch (Exception ex) {
throw ex;
}
}
推荐答案
您已在使用C#访问,读取/修改委托电子邮件或共享Gmail的Gmail API [ ^ ]。请不要重新发布。
You already posted this question at Gmail API to access, read/modify the delegated emails or shared gmail using C#[^]. Please do not repost.
这篇关于如何使用C#中的服务帐户从gmail访问委派的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文