我正在尝试从Google API中获取人员信息。
以下是代码
public static void main(String[] args) throws IOException, JSONException
{
plus = new Plus.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
public void initialize(HttpRequest request) throws IOException {
}
}).setApplicationName("youtubeSearchSample").build();
Person profile = plus.people().get("https://www.googleapis.com/plus/v1/people/118359906569721392650?key=<Key>").execute();
System.out.println("hello-->"+profile.toPrettyString());
}
public static Credential authorize(List<String> scopes, String credentialDatastore) throws IOException {
// Load client secrets.
InputStreamReader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("/client_secrets.json"));
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, clientSecretReader);
// Checks that the defaults have been replaced (Default = "Enter X here").
if (clientSecrets.getDetails().getClientId().startsWith("Enter")
|| clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
System.out.println(
"Enter Client ID and Secret from https://code.google.com/apis/console/?api=youtube"
+ "into src/main/resources/client_secrets.json");
System.exit(1);
}
// This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore}
FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY));
DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore(credentialDatastore);
System.out.println("scopes-->"+scopes.toString());
System.out.println("clientSecrets-->"+clientSecrets.toString());
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setCredentialDataStore(datastore)
.build();
// Build the local server and bind it to port 8080
LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();
// Authorize.
return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user");
}
当我尝试运行上面的代码时,我遇到以下异常。
Exception in thread "main" java.lang.NoSuchMethodError: com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient.<init>(Lcom/google/api/client/http/HttpTransport;Lcom/google/api/client/http/HttpRequestInitializer;Ljava/lang/String;Ljava/lang/String;Lcom/google/api/client/json/JsonObjectParser;Lcom/google/api/client/googleapis/services/GoogleClientRequestInitializer;Ljava/lang/String;Z)V
at com.google.api.services.plus.Plus.<init>(Plus.java:134)
at com.google.api.services.plus.Plus$Builder.build(Plus.java:2086)
at com.google.youtube.search.GoogleUserDetails.main(GoogleUserDetails.java:32)
我在为YouTube.Builder尝试过的时候,它工作得很好。
以下是我用于youtube builder的示例代码。
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new
HttpRequestInitializer() {
public void initialize(HttpRequest request) throws IOException {
}
}).setApplicationName("youtubeSearchSample").build();
我刚刚用Plus.Builder替换了YouTube.Builder,但我得到了上面提到的上述异常。
任何GoogleAPI专家都会让我犯错。
Pom.xml依赖项:
<dependencies>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-youtube</artifactId>
<version>${project.youtube.version}</version>
</dependency>
<!-- Required for any code that makes calls to the Google Analytics API -->
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-youtubeAnalytics</artifactId>
<version>${project.youtube.analytics.version}</version>
</dependency>
<!-- This dependency is only used for the Topics API sample, which requires
the Jackson JSON parser -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
<version>${project.http.version}</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>${project.oauth.version}</version>
</dependency>
<dependency>
<groupId>com.google.collections</groupId>
<artifactId>google-collections</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
<!-- Google API dependency -->
<dependency>
<groupId>com.googlecode.googleplus</groupId>
<artifactId>google-plus-java-api</artifactId>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-plus</artifactId>
<version>v1-rev64-1.13.2-beta</version>
</dependency>
</dependencies>
答案 0 :(得分:0)
我通过更新pom.xml中的api库版本来修复此错误。
然后你应该打电话给
mvn clean package
您可以在Maven存储库中找到最新版本的Google库:
http://mvnrepository.com/artifact/com.googlecode.googleplus/google-plus-java-api