简体   繁体   English

使用服务帐户对 Gmail 进行 OAuth2 身份验证

[英]Use service account for OAuth2 authentication to gmail

I need to implement OAuth2 authentication in my app for gmail.我需要在我的 gmail 应用程序中实现 OAuth2 身份验证。 App is running in a background without UI.应用程序在没有 UI 的后台运行。 So I have google account (I think that's not a GSuite).所以我有谷歌帐户(我认为这不是 GSuite)。 I created a Service Account following the instructions here: https://www.emailarchitect.net/easendmail/sdk/html/object_oauth_service_account.htm (but without assigning a product - I didn't have an option for that. May it be because my account is not GSuite?..).我按照此处的说明创建了一个服务帐户: https : //www.emailarchitect.net/easendmail/sdk/html/object_oauth_service_account.htm (但没有分配产品 - 我没有选择。可能是因为我的帐户不是 GSuite?..)。 After that I created a key with JSON file for JWT authentication.之后,我使用 JSON 文件创建了一个用于 JWT 身份验证的密钥。 I'm using google-auth-library-oauth2-http library to generate access_token and use it to login to gmail mailbox.我正在使用google-auth-library-oauth2-http库生成access_token并使用它登录到 gmail 邮箱。 Here's the code snippet for token generation:这是令牌生成的代码片段:

GoogleCredentials credentials = ServiceAccountCredentials.fromStream(new FileInputStream("path_to_json")
                                     .createScoped(Arrays.asList("https://mail.google.com"));
credentials.refreshIfExpired();
AccessToken accessToken = credentials.getAccessToken();

AccessToken is retrieved successfully but when I'm trying to use that for mailbox authentication I'm getting javax.mail.AuthenticationFailedException: [AUTHENTICATIONFAILED] Invalid credentials (Failure) . AccessToken 已成功检索,但是当我尝试将其用于邮箱身份验证时,我收到javax.mail.AuthenticationFailedException: [AUTHENTICATIONFAILED] Invalid credentials (Failure)

Here's the code snippet for mailbox connection:这是邮箱连接的代码片段:

Properties props = new Properties();
props.put("mail.imap.ssl.enable", "true");
props.put("mail.imap.auth.mechanisms", "XOAUTH2");
Session session = Session.getInstance(props);
Store store = session.getStore("imap");
store.connect("imap.gmail.com", "my_acc@gmail.com", access_token);

The question is whether it's possible to authenticate to gmail mailbox with access token via JWT generated basing on service account data?问题是是否可以通过基于服务帐户数据生成的 JWT 使用访问令牌对 gmail 邮箱进行身份验证? Is it even possible?甚至有可能吗?

You can create a Service Account without G Suite, but you do need it to use "Domain-wide delegation" if you want to access a user's Gmail inbox.您可以在没有 G Suite 的情况下创建服务帐户,但如果您想访问用户的 Gmail 收件箱,则需要它来使用“域范围委派”。 As you can read in the documentation :正如您在文档中所读到的:

In enterprise applications you may want to programmatically access a user's data without any manual authorization on their part.在企业应用程序中,您可能希望以编程方式访问用户的数据,而无需他们进行任何手动授权。 In G Suite domains, the domain administrator can grant third-party applications with domain-wide access to its users' data — this is known as domain-wide delegation of authority.在 G Suite域中,域管理员可以授予第三方应用对其用户数据的全域访问权限——这称为全域授权。 To delegate authority this way, domain administrators can use service accounts with OAuth 2.0.为了以这种方式委派权限,域管理员可以使用 OAuth 2.0 的服务帐户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 google oauth2使用user@gmail.com模拟服务帐户 - google oauth2 impersonate service account with user@gmail.com 在GAE Java应用程序中使用OAuth2身份验证通过IMAP访问用户GMail帐户 - Access user GMail account via IMAP using OAuth2 authentication in GAE Java application Java中的Adwords API,OAuth2和服务帐户 - Adwords API, OAuth2 and service account in Java 选择Gmail帐户oAuth2后,应用程序崩溃了吗? - App Crashing after selecting Gmail Account oAuth2? OAuth2身份验证服务的集成测试? - Integration tests of an OAuth2 authentication service? 具有自定义身份验证提供程序的 OAUTH2 用户服务 - OAUTH2 user service with Custom Authentication Providers Javamail gmail 和 OAuth2 - Javamail gmail and OAuth2 使用OAuth2对Google App Engine Java中不同服务提供商的用户进行身份验证 - Use OAuth2 to authentication users of different service providers in Google app engine Java Spring Security OAuth2 - 如何使用 OAuth2Authentication 对象? - Spring Security OAuth2 - How to use OAuth2Authentication object? 带有Google Speech API RPC服务帐户的Google Oauth2无法正常工作 - Google Oauth2 with Service account for Google Speech API rpc not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM