google plus java api - Anfänger sucht Hilfe

Mattze_1987

Mitglied
Hallo liebes Forum,

Ich möchte mit Hilfe der google+ API meinen Google+ Account auslesen. Ich habe dazu folgendes Codebeispiel aus der Google+ Dokumentation versucht zu implementieren:

Java:
// Set up the HTTP transport and JSON factory
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();

// Set up OAuth 2.0 access of protected resources
// using the refresh and access tokens, automatically
// refreshing the access token when it expires
GoogleAccessProtectedResource requestInitializer =
    new GoogleAccessProtectedResource(accessToken, httpTransport,
    jsonFactory, clientId, clientSecret, refreshToken);

// Set up the main Google+ class
Plus plus = Plus.builder(httpTransport, jsonFactory)
    .setHttpRequestInitializer(requestInitializer)
    .build();

// Make a request to access your profile and display it to console
Person profile = plus.people().get("me").execute();
System.out.println("ID: " + profile.getId());
System.out.println("Name: " + profile.getDisplayName());
System.out.println("Image URL: " + profile.getImage().getUrl());
System.out.println("Profile URL: " + profile.getUrl());

Quelle: SampleProgram - google-api-java-client - Short sample program. - Google APIs Client Library for Java - Google Project Hosting

Bei Ausführen des Beispielcodes wird folgende Exception geworfen:

Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/jackson/JsonFactory
at com.google.api.client.json.jackson.JacksonFactory.<init>(JacksonFactory.java:43)
at SocialMediaProject.googleapi.<init>(googleapi.java:21)
at SocialMediaProject.googleapi.main(googleapi.java:37)
Caused by: java.lang.ClassNotFoundException: org.codehaus.jackson.JsonFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more

Für die JacksonFactory wird scheinbar noch etwas benötigt vermutlich muss diese Dateien:
http://samples.google-api-java-clie...mdline/src/main/resources/client_secrets.json
irgendwie eingebunden werden. Aber ich habe keine Ahnung wie das gehen soll und die google+ Dokumentation ist auch irgendwie nicht hilfreich. Ich wäre hier für gute Tipps sehr dankbar.

Gruß
 
Lustigerweise ist auf der dazugehörigen Seite der Downloadlink im Eimer. Hat zufällig jemand eine alternative Quelle, ich google mich grad schon zu Tode 🙂

Edit: Alles klar, habs gefunden.
 
Zuletzt bearbeitet:
Ich versuche an den access_token ran zu kommen und habe dazu folgenden http post erstellt. Wenn ich das ganze ausführe bekomme ich immer folgende Fehlermeldung:
Java:
<HTML>
<HEAD>
<TITLE>Client must specify either client_id or client_assertion, not both</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Client must specify either client_id or client_assertion, not both</H1>
<H2>Error 400</H2>
</BODY>
</HTML>

public class Test {
	public static void main(String[] args) {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(
				"https://accounts.google.com/o/oauth2/token");

		try {
			 final HttpEntity entity = new UrlEncodedFormEntity(Arrays.asList(
	                    new BasicNameValuePair("cliend_id", "xxxxxxxxxxxxx"),
	                    new BasicNameValuePair("client_secret", "xxxxxxxxxxx"),
	                    new BasicNameValuePair("redirect_uri","xxxxxxxxx"),
	                    new BasicNameValuePair("grant_type", "authorization_code"),
	                    new BasicNameValuePair("code", "xxxxxxxx")));
	            	post.setEntity(entity);			

			HttpResponse response = client.execute(post);
			StringBuilder builder = new StringBuilder();
			String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
	    	while((line = reader.readLine()) != null) {
	    	 builder.append(line);
	    	 System.out.println(line);
	    	}
	    	
	    	JSONObject json = null;
			try {
				json = new JSONObject(builder.toString());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	System.out.println(json);

			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
 

Zurück
Oben