我正在尝试在用户的Google+帐户中创建一个圈子。我按照快速启动(https://developers.google.com/+/domains/quickstart/java)配置服务帐户及其凭据,并将域范围的权限委派给服务帐户。当我运行代码时,我得到404 Not found响应。
这是我的代码:
package com.google.plus.samples.quickstart.domains;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.plusDomains.PlusDomains;
import com.google.api.services.plusDomains.model.Acl;
import com.google.api.services.plusDomains.model.Activity;
import com.google.api.services.plusDomains.model.PlusDomainsAclentryResource;
import com.google.api.services.plusDomains.model.Circle;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.services.plusDomains.model.Person;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class DomainDelegation {
private static final String SERVICE_ACCOUNT_EMAIL = "serviceaccountemail@developer.gserviceaccount.com";
private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH =
"privatekeypath.p12";
private static final String USER_EMAIL = "user1@mygoogledomain.com";
private static final List<String> SCOPE = Arrays.asList(
"https://www.googleapis.com/auth/plus.me",
"https://www.googleapis.com/auth/plus.stream.write",
"https://www.googleapis.com/auth/plus.circles.read",
"https://www.googleapis.com/auth/plus.circles.write",
"https://www.googleapis.com/auth/plus.login");
private static PlusDomains authenticate() throws GeneralSecurityException, IOException {
System.out.println(String.format("Authenticate the domain for %s", USER_EMAIL));
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(SCOPE)
.setServiceAccountUser(USER_EMAIL)
.setServiceAccountPrivateKeyFromP12File(
new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
.build();
// Create and return the Plus service object
PlusDomains service = new PlusDomains.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("Test")
.setHttpRequestInitializer(credential).build();
return service;
}
public static void main(String[] args) throws Exception {
// Create an authorized API client
PlusDomains service = authenticate();
Circle circle = new Circle();
circle.setDisplayName("Tech support");
Circle result = service.circles().insert("me", circle).execute();
System.out.println("Created 'Tech support' circle with id: " + result.getId());
}
}
以下是我得到的回复:
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
Not Found
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:312)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1045)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
at com.google.plus.samples.quickstart.domains.DomainDelegation.main(DomainDelegation.java:130)
如何修复错误或找出找不到的页面?
答案 0 :(得分:0)
我通过使用以下代码替换默认网址来解决问题:
PlusDomains service = new PlusDomains.Builder(httpTransport,
jsonFactory, credential).setApplicationName("Test")
.setRootUrl("https://www.googleapis.com/")
.setHttpRequestInitializer(credential).build();