我正在尝试使用google java api服务发送基于Gmail REST API的邮件.我已通过Google Develover Console配置了应用程序客户端并下载了p12和json文件.
我使用过这个示例程序,https://developers.google.com/gmail/api/guides/sending#sending_messages ...
此示例有效,但这是基于GoogleAuthorizationCodeFlow.我只想从服务器到服务器工作,直接调用,而不是打开浏览器来获取访问令牌......我得到了它(访问令牌)但最后我收到了错误请求....为什么? ?我收到的信息不仅仅是"Bad Request"和"Precondition Failed"
基于此,我按照以下步骤操作:
第一步:根据我的客户帐户邮件和p12生成的文件创建GoogleCredential对象:
GoogleCredential credential = new GoogleCredential.Builder().setTransport(new NetHttpTransport())
.setJsonFactory(new JacksonFactory())
.setServiceAccountId(serviceAccountUserEmail)
.setServiceAccountScopes(scopes)
.setServiceAccountPrivateKeyFromP12File(
new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
.build();
Run Code Online (Sandbox Code Playgroud)在这里,我必须指出,我有很多问题需要使用ClientID而不是ClientMail.它必须使用@ developer.gserviceaccount.com帐户而不是.apps.googleusercontent.com.如果你不发送这个参数确定,你得到一个"无效授予"错误.这在此处说明:https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtAuthorization
第二步:根据凭据创建Gmail服务:
Gmail gmailService = new Gmail.Builder(httpTransport,
jsonFactory,
credential)
.setApplicationName(APP_NAME)
.build();
Run Code Online (Sandbox Code Playgroud)第三步从MimmeMessage创建Google原始消息:
private static Message _createMessageWithEmail(final MimeMessage email) throws MessagingException, IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
email.writeTo(bytes);
String encodedEmail = Base64.encodeBase64URLSafeString(bytes.toByteArray());
Message message = new Message();
message.setRaw(encodedEmail);
return message;
Run Code Online (Sandbox Code Playgroud)
}
第四步调 …
是否可以在不使用模拟的情况下使用Google Credentials for GMAIL REST API?
在GMAIL REST API的许多示例中,我看到必须使用与域和Google APPS关联的模拟帐户.我只想使用GMAIL REST API api服务器到服务器:
FE:
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(serviceAccountUserEmail)
**.setServiceAccountUser("myuser@mydomain.com)**
.setServiceAccountScopes(SCOPES)
.setServiceAccountPrivateKeyFromP12File(
new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
.build();
Run Code Online (Sandbox Code Playgroud)
问题是,我没有任何域名,我只使用GMAIL帐户...但我没有任何方式授权,例如:MyMainGmailAcccoun@gmail.com
servive acccount id 4xxxxxxxxxxxxxq@developer.gserviceaccount.com";是使用MyMainGmailAcccoun@gmail.com创建的" 客户帐户"
这没有任何意义,我不想冒充其他域名的其他帐户,我也没有任何办法来authozize MyMainGmailAcccoun@gmail.com.
只是有效,如果您有一个帐户来冒充与Google Apps相关的其他域名,并获得Google Apps ...您需要一个域名.
任何建议?