gpt4 book ai didi

c# - 客户端无权使用此方法检索访问 token Gmail API C#

转载 作者:可可西里 更新时间:2023-11-01 03:09:02 25 4
gpt4 key购买 nike

当我尝试使用服务帐户授权 gmail api 时出现以下错误

"Client is unauthorized to retrieve access tokens using this method"

static async Task MainAsync()
{

sstageEntities db = new sstageEntities();
//UserCredential credential;
Dictionary<string, string> dictionary = new Dictionary<string, string>();
String serviceAccountEmail =
"xxx.iam.gserviceaccount.com";

var certificate = new X509Certificate2(
AppDomain.CurrentDomain.BaseDirectory +
"xxx-8c7a4169631a.p12",
"notasecret",
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);

//string userEmail = "user@domainhere.com.au";

ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
User = "xxx@xxx.com",
Scopes = new[] { "https://mail.google.com/" }
}.FromCertificate(certificate)
);


// Create Gmail API service.
var gmailService = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});

// Define parameters of request.

var emailListRequest = gmailService.Users.Messages.List("xxx@xxx.com");
emailListRequest.LabelIds = "INBOX";
emailListRequest.IncludeSpamTrash = true;
emailListRequest.Q = "from:bpm@xxx.co.in is:unread";



//Get our emails
var emailListResponse = await emailListRequest.ExecuteAsync();

我正在使用创建服务帐户时获得的 p12 key 。但是当我运行我的控制台应用程序时,出现以下错误。非常感谢任何帮助。

提前致谢!

最佳答案

服务帐户需要授权,否则无法访问该域的电子邮件。

"Client is unauthorized to retrieve access tokens using this method"

表示您没有正确授权检查Delegating domain-wide authority to the service account

关于c# - 客户端无权使用此方法检索访问 token Gmail API C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42784640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com