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.