2

Im new to Google+ API and Java-Client API. so Please be patient with me .

Im trying to retrieve my google+ activities using Java-Client and G+ APIs 1.14 but I failed to create the client object, Ive been trying for a day but I havnt had luck to solve it myslef So I came here to ask for a help.

My code is:

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.services.CommonGoogleClientRequestInitializer;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;
import com.google.api.client.googleapis.services.json.CommonGoogleJsonClientRequestInitializer;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.plus.Plus;
import com.google.api.services.plus.model.Activity;
import com.google.api.services.plus.model.ActivityFeed;
import com.google.api.services.plus.Plus.Builder;

public class GooglePlusService {

      /**
       * google plus service acess stub
       */
      private Plus plusSvc;

      /**
       * google plus api key
       */
      private String GooglePlusAPIKey = "MY key"; //<-- YOUR KEY GOES HERE

      /**
       * connect to the google plus service
       */
      public GooglePlusService() {

        setupTransport();

      }

      public List<Activity> getActivityList(String userid) throws IOException {

        List<Activity> retval = new ArrayList<Activity>();

        Plus.Activities.List listActivities =
            plusSvc.activities().list(userid, "public");

        listActivities.setMaxResults(100L);

        // get the 1st page of activity objects
        ActivityFeed activityFeed = listActivities.execute();

        // unwrap the request and extract the pieces we want
        List<Activity> pageOfActivities = activityFeed.getItems();

        // loop through until we arrive at an empty page
        while (pageOfActivities != null) {
          for (Activity activity : pageOfActivities) {
            retval.add(activity);
            System.out.println("ID " + activity.getId() + " Content: " +
                               activity.getObject().getContent());
          }

          // we will know we are on the last page when the next page token
          // is null (in which case, break).
          if (activityFeed.getNextPageToken() == null) {
            break;
          }

          // prepare to request the next page of activities
          listActivities.setPageToken(activityFeed.getNextPageToken());

          // execute and process the next page request
          activityFeed = listActivities.execute();
          pageOfActivities = activityFeed.getItems();
        }

        return retval;

      }

      /**
       * google plus service stub object
       */
      public Plus getPlusSvc() {
        return plusSvc;
      }

      private void setupTransport() {

          /*
plusSvc = new Plus(new NetHttpTransport(),
                       new GsonFactory());
    plusSvc.setKey(GooglePlusAPIKey);

                */


          HttpRequestInitializer initializer =  (HttpRequestInitializer) new  CommonGoogleClientRequestInitializer("GooglePlusAPIKey"); 

          plusSvc = new Plus(new NetHttpTransport(),  new GsonFactory(), initializer);

          GoogleClientRequestInitializer KEY_INITIALIZER = new CommonGoogleJsonClientRequestInitializer(GooglePlusAPIKey);     
      }


}

My problem is in setupTransport() section, I failed to create the client object cuz I didnt know what to assign to httpRequestInitializer in :

Plus(HttpTransport transport,JsonFactory jsonFactory,HttpRequestInitializer httpRequestInitializer)

I tried : HttpRequestInitializer initializer = (HttpRequestInitializer)new CommonGoogleClientRequestInitializer("GooglePlusAPIKey");
But it gave error of failing to cast CommonGoogleClientRequestInitializer

I tried : GoogleClientRequestInitializer KEY_INITIALIZER = new CommonGoogleJsonClientRequestInitializer(GooglePlusAPIKey); But didnt know what to do after that.

I need to understand how do the http request initializer using the APIs

I really need a help, I appreciate it.

Waiting for the response.

2 Answers 2

3

The HttpRequestInitializer instance is meant to be a GoogleCredential object, as demonstrated in the PhotoHunt sample. The GoogleCredential object will set the relevant Authorization headers on each request of the Plus service before sending the request.

The relevant lines to create the GoogleCredential object and use it in the Plus.Builder are:

GoogleCredential credential = new GoogleCredential.Builder()
    .setJsonFactory(JSON_FACTORY).setTransport(TRANSPORT)
    .setClientSecrets(CLIENT_ID, CLIENT_SECRET).build();
GoogleTokenResponse tokenFromExchange = exchangeCode(accessToken);
credential.setFromTokenResponse(tokenFromExchange);
Plus plus = new Plus.Builder(TRANSPORT, JSON_FACTORY, credential).build();
0
0

Thank you very much, The problem is solved :))..

This is the solution:

   GoogleCredential credential = new GoogleCredential();
            //plusSvc = new Plus(new NetHttpTransport(),  new GsonFactory(), credential);
            plusSvc =  new Plus.Builder(new NetHttpTransport(),  new GsonFactory(), credential)
            .setApplicationName("Meetrbus/1.0")
            .setHttpRequestInitializer(credential)
            .setPlusRequestInitializer(
                    new PlusRequestInitializer(GooglePlusAPIKey) ).build();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.