1

I am trying to use the Google generated API library for the first time for Android. I have taken code from the the Sample Program provided by Google. My code looks like

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;

// Core Google API
import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAuthorizationRequestUrl;
import com.google.api.client.http.ByteArrayContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
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;

// Google+ API
import com.google.api.services.plus.*;

public class GooglePlusActivity extends Activity {

    // Want data about authenticated user
    private static final String SCOPE = "https://www.googleapis.com/auth/plus.me";
    //
    private static final String CALLBACK_URL = "urn:ietf:wg:oauth:2.0:oob";
    //
    private static final HttpTransport TRANSPORT = new NetHttpTransport();
    private static final JsonFactory JSON_FACTORY = new JacksonFactory();

    private static final String CLIENT_ID = "XXXXXXXXXXXXXXX";
    private static final String CLIENT_SECRET = "XXXXXXXXXXXXXX";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Generate the URL to which we will direct users
        String authorizeUrl = new GoogleAuthorizationRequestUrl(CLIENT_ID,
                CALLBACK_URL, SCOPE).build();

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        try {
            String authorizationCode = in.readLine();

            // Exchange for an access and refresh token
            GoogleAuthorizationCodeGrant authRequest = new GoogleAuthorizationCodeGrant(TRANSPORT,
                JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, authorizationCode, CALLBACK_URL);

            authRequest.useBasicAuthorization = false;

            AccessTokenResponse authResponse = authRequest.execute();

            String accessToken = authResponse.accessToken;

            GoogleAccessProtectedResource access = new GoogleAccessProtectedResource(accessToken,
                TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, authResponse.refreshToken);

            HttpRequestFactory rf = TRANSPORT.createRequestFactory(access);
            System.out.println("Access token: " + authResponse.accessToken);


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Plus plus = new Plus(new NetHttpTransport(), new JacksonFactory());

    }
}

However the following line:

private static final HttpTransport TRANSPORT = new NetHttpTransport();

cause an exception:

10-04 13:33:28.954: ERROR/AndroidRuntime(5925): Caused by: java.lang.NoClassDefFoundError: com.google.api.client.http.javanet.NetHttpTransport

I have the following libraries added to the build path:

google-api-client-1.5.0-beta.jar google-api-services-plus-v1-1.2.2-beta.jar

and these dependency libraries:

guava-r09 httpclient-4.0.3 jackson-core-asl-1.6.7 gson-1.6

I added the dependencies only out of desperation...since I cannot see that I need them. All my import statements are resolved correctly at compile time, so why would I get this error?

I'm using Eclipse Indigo on Windows 7.

I added the following jar files:

google-api-client-1.5.0-beta
google-api-client-extensions-1.5.0-beta
google-api-client-extensions-android2-1.5.0-beta
google-api-services-plus-v1-1.2.2-beta
google-http-client-1.5.0-beta
google-http-client-extensions-1.5.0-beta
google-http-client-extensions-android2-1.5.0-beta
google-oauth-client-1.5.0-beta
google-oauth-client-extensions-1.5.0-beta
gson-1.6
guava-r09
httpclient-4.0.3
httpcore-4.0.1
jackson-core-asl-1.6.7

this resolved my problem. However I am still unsure about how these files correspond to the libraries list on the wiki page and also why they're required when my imports are resolved during compile time.

Jack
  • 10,313
  • 15
  • 75
  • 118
  • Did you ever find out the how and the why? I'm having the exact same problem, and although I'm using slightly different versions (1.6.0), I can't seem to fix it even though I've added above list (mainly the httpclient an dhttpcore needed to be added) to my project. But no dice :( – Nanne Jan 08 '12 at 13:49

4 Answers4

6

You must explicit add library reference of google-http-client-1.5.0-beta.jar(right click project->Properties->Java Build Path->Add External JARs), don't believe class path. If you do not do this, in the apk file does not contain the required data。
You can download Google plus library from here google-plus-java-starter_v5.zip enter image description here

Sridhar Nalam
  • 527
  • 8
  • 9
meizilp
  • 3,911
  • 3
  • 28
  • 11
0

Here is a list of the libraries you should have in you project: http://code.google.com/p/google-api-java-client/wiki/Setup

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • The zip file offered by Google on that same page does not contain all of the jar file listed. For example, I cannot find the jar file for: google-api-client-googleapis google-api-client-googleapis-extensions google-api-client-googleapis-extensions-android2 google-api-client-extensions-android3 – Jack Oct 04 '11 at 13:19
0

May be a bit late for this one, but the the jar that you're looking for is google-http-client-1.5.0-beta.jar

This jar defines com.google.api.client.http.* and com.google.api.client.json.*

martyhu
  • 161
  • 1
  • 5
0
plus = Plus.builder(transport, jsonFactory).setApplicationName("Google-PlusSample/1.0")
    .setHttpRequestInitializer(accessProtectedResource)
    .setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
        @Override
        public void initialize(JsonHttpRequest request) {
            PlusRequest plusRequest = (PlusRequest) request;
            plusRequest.setPrettyPrint(true);
        }
    }).build();

use this instead of

Plus plus = new Plus(new NetHttpTransport(), new JacksonFactory());
sabadow
  • 5,095
  • 3
  • 34
  • 51
nicky
  • 3,810
  • 9
  • 35
  • 44