c# - 使用服务帐户的 Gmail API 集转发
问题描述
任何人都可以帮助我在调用 Gmail API 设置转发地址时遇到错误的请求失败前提条件错误?下面是我正在尝试使用的 C# .Net 控制台应用程序。我已将域范围的权限委托给服务帐户。
错误:
Google.Apis.Requests.RequestError 错误请求 [400] 错误 [ Message[Bad Request] Location[ - ] Reason[failedPrecondition] Domain[global] ]
我想我错过了冒充用户。所以,我添加了用户,现在我收到以下错误。
错误:
错误:“unauthorized_client”,描述:“客户端未经授权无法使用此方法检索访问令牌。”,Uri:“”
namespace GmailForwarder
{
class Program
{
static string ApplicationName = "GmailForwarder";
static void Main(string[] args)
{
ServiceAccountCredential credential;
string serviceAccount = "gmailforwarder@gmailforwarder.iam.gserviceaccount.com";
var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);
try
{
// Create credential
credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccount)
{
User = "wtestboonew@chicagobooth.edu",
Scopes = new[] { GmailService.Scope.GmailSettingsSharing }
}.FromCertificate(certificate));
// Create Gmail API service.
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
string user = "wtestboonew@chicagobooth.edu"; // test gmail account
string fwdAddr = "acw5274@gmail.com";
ForwardingAddress fwdAddress = new ForwardingAddress();
fwdAddress.ForwardingEmail = fwdAddr;
var createFwdAddressResult = service.Users.Settings.ForwardingAddresses.Create(fwdAddress,"me").Execute();
}
catch (Exception ex)
{
}
}
}
}
解决方案
当我使用时,这对我有用:https ://mail.google.com/和https://www.googleapis.com/auth/gmail.settings.sharing OAuth2 范围
推荐阅读
- php - 使用php从json中的嵌套数组中获取数据
- typescript - 打字稿在linux获取模块未找到错误中表现不同
- core-data - 线程 1:“NSFetchedResultsController 的一个实例需要一个带有排序描述符的获取请求”
- terraform - Terraform 中动态入口中的 Rule_nos
- python - 网页抓取到 html 中的特定点然后停止
- go - 有没有办法改变分配给函数的结构通道?
- ios - 加载的 Xib 总是在出口后立即崩溃
- dynamic - SwiftUi 值转换为 let 语句
- sql - 如何在给定表的列之间执行差异
- javascript - JavaScript 变量名如何链接到内存地址?