我们可以使用服务帐户访问GMAIL API吗?

我有一个桌面应用程序,通过REST接口使用GMAIL API读取邮件。 我想使用服务帐户,以便我们可以使用域设置下载邮件,用户交互为空。 我已成功创建Gmail服务实例,但当我尝试访问任何Gmail API方法(例如提取邮件列表或其他任何内容)时,我会收到例外说法

Google.Apis.Auth.OAuth2.Responses.TokenResponseException:错误:“access_denied”,说明:“请求的客户端未经授权。”

我完成了开发人员控制台的所有设置,并将范围添加到我的gapps域。

Gmail API是否支持服务帐户? 使用相同的设置和服务帐户,我可以使用云服务和API获取Google云端硬盘中所有文件的列表。

我使用以下C#代码从服务帐户访问Gmail

String serviceAccountEmail = "999999999-9nqenknknknpmdvif7onn2kvusnqct2c@developer.gserviceaccount.com"; var certificate = new X509Certificate2( AppDomain.CurrentDomain.BaseDirectory + "certs//fe433c710f4980a8cc3dda83e54cf7c3bb242a46-privatekey.p12", "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable); string userEmail = "user@domainhere.com.au"; ServiceAccountCredential credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(serviceAccountEmail) { User = userEmail, Scopes = new[] { "https://mail.google.com/" } }.FromCertificate(certificate) ); if (credential.RequestAccessTokenAsync(CancellationToken.None).Result) { GmailService gs = new GmailService( new Google.Apis.Services.BaseClientService.Initializer() { ApplicationName = "iLink", HttpClientInitializer = credential } ); UsersResource.MessagesResource.GetRequest gr = gs.Users.Messages.Get(userEmail, msgId); gr.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Raw; Message m = gr.Execute(); if (gr.Format == UsersResource.MessagesResource.GetRequest.FormatEnum.Raw) { byte[] decodedByte = FromBase64ForUrlString(m.Raw); string base64Encoded = Convert.ToString(decodedByte); MailMessage msg = new MailMessage(); msg.LoadMessage(decodedByte); } } 

如果您想“阅读邮件”,则需要使用较新的Gmail API(而不是指出“以二进制方式丢失”的旧管理员设置API)。 是的,您可以使用oauth2和较新的Gmail API执行此操作,您需要将Cpanel中的开发人员列入白名单并创建一个可以用来签署请求的密钥 – 需要一些设置: https : //developers.google.com /帐号/文档/ OAuth2ServiceAccount#formingclaimset

是的,你可以…检查授权设置……

https://developers.google.com/admin-sdk/directory/v1/guides/delegation#delegate_domain-wide_authority_to_your_service_account

编辑:使用Eric DeFriez分享的链接。

您可以使用新的Gmail API访问任何user@YOUR_DOMAIN.COM邮件/标签/广告代码等:

https://developers.google.com/gmail/api/

通过模拟的服务帐户(服务帐户正在访问API,就像它是您域中的特定用户一样)。

请在此处查看详细信息: https : //developers.google.com/identity/protocols/OAuth2ServiceAccount

这是Dartlang中的相关代码:

 import 'package:googleapis_auth/auth_io.dart' as auth; import 'package:googleapis/gmail/v1.dart' as gmail; import 'package:http/http.dart' as http; ///credentials created with service_account here https://console.developers.google.com/apis/credentials/?project=YOUR_PROJECT_ID final String creds = r''' { "private_key_id": "FILL_private_key_id", "private_key": "FILL_private_key", "client_email": "FILL_service_account_email", "client_id": "FILL_client_id", "type": "service_account" }'''; Future createImpersonatedClient(String impersonatedUserEmail, List scopes) async { var impersonatedCredentials = new auth.ServiceAccountCredentials.fromJson(creds,impersonatedUser: impersonatedUserEmail); return auth.clientViaServiceAccount(impersonatedCredentials , scopes); } getUserEmails(String userEmail) async { //userEmail from YOUR_DOMAIN.COM var client = await createImpersonatedClient(userEmail, [gmail.GmailApi.MailGoogleComScope]); var gmailApi = new gmail.GmailApi(client); return gmailApi.users.messages.list(userEmail, maxResults: 5); }