5
我正嘗試使用服務帳戶在JUnit測試中簡單發送來自gmail的電子郵件。Gmail Api服務帳戶給出失敗的先決條件回覆
我有這樣的測試:
@Test
public void testGmailCredential() throws GeneralSecurityException, URISyntaxException {
try {
InputStream is = getClass().getClassLoader().getResourceAsStream("myProject.json");
GoogleCredential credential = GoogleCredential.fromStream(is).createScoped(GmailScopes.all());
System.out.println("Scopes are " + credential.getServiceAccountScopesAsString());
Gmail gmailService = new Gmail.Builder(getHttpTransport(), JSON_FACTORY, credential)
.setApplicationName("WHAT SHOULD THIS VALUE BE?")
//.setHttpRequestInitializer(credential)
.build();
InternetAddress to = new InternetAddress("[email protected]", "Tom Catfish");
InternetAddress from = new InternetAddress("[email protected]", "Some Name");
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage email = new MimeMessage(session);
email.setFrom(from);
email.addRecipient(javax.mail.Message.RecipientType.TO, to);
email.setSubject("Subject here");
email.setText("body here");
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
email.writeTo(buffer);
byte[] bytes = buffer.toByteArray();
String encodedEmail = Base64.getUrlEncoder().encodeToString(bytes);
Message message = new Message();
message.setRaw(encodedEmail);
message = gmailService.users().messages().send("me", message).execute();
System.out.println("Message id: " + message.getId());
System.out.println(message.toPrettyString());
// other code...
}
從而未能與此:
Scopes are https://www.googleapis.com/auth/gmail.compose
com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Bad Request",
"reason" : "failedPrecondition"
} ],
"message" : "Bad Request"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at com.op.services.NotificationServiceTest.testGmailCredential(NotificationServiceTest.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
我有一個包含此信息的文件src/test/resources/myProject.json
:
{
"type": "service_account",
"project_id": "xxxxxxxxxxx",
"private_key_id": "28341330...",
"private_key": "-----BEGIN PRIVATE KEY-----\.........\n-----END PRIVATE KEY-----\n",
"client_email": "[email protected]",
"client_id": "1182....",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/myApp%40xxxxxxx.iam.gserviceaccount.com"
}
我怎麼能解決什麼是造成這種愚蠢的,從谷歌沒有幫助failedPrecondition resopnse?
服務器回答以http 400問題是:哪個服務器;您是否使用了代理?你能嗅探網絡流量嗎? – rds
由於您擁有googleapis客戶端的代碼源,因此可以在AbstractGoogleClientRequest interceptResponse()中放置breakpint以查看有關響應的更多詳細信息。 – rds