Example usage for com.google.api.client.googleapis.services CommonGoogleClientRequestInitializer CommonGoogleClientRequestInitializer

List of usage examples for com.google.api.client.googleapis.services CommonGoogleClientRequestInitializer CommonGoogleClientRequestInitializer

Introduction

In this page you can find the example usage for com.google.api.client.googleapis.services CommonGoogleClientRequestInitializer CommonGoogleClientRequestInitializer.

Prototype

protected CommonGoogleClientRequestInitializer(Builder builder) 

Source Link

Usage

From source file:com.gdgdevfest.android.apps.devfestbcn.io.AnnouncementsFetcher.java

License:Apache License

public ArrayList<ContentProviderOperation> fetchAndParse() throws IOException {
    final ArrayList<ContentProviderOperation> batch = Lists.newArrayList();

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

    // Set up the main Google+ class
    Plus plus = new Plus.Builder(httpTransport, jsonFactory, null)
            .setApplicationName(NetUtils.getUserAgent(mContext))
            .setGoogleClientRequestInitializer(new CommonGoogleClientRequestInitializer(Config.API_KEY))
            .build();/*from w  w w .ja  va 2 s .c  o m*/

    ActivityFeed activities;
    try {
        activities = plus.activities().list(Config.ANNOUNCEMENTS_PLUS_ID, "public").setMaxResults(100l)
                .execute();
        if (activities == null || activities.getItems() == null) {
            throw new IOException("Activities list was null.");
        }

    } catch (IOException e) {
        LOGE(TAG, "Error fetching announcements", e);
        return batch;
    }

    LOGI(TAG, "Updating announcements data");

    // Clear out existing announcements
    batch.add(ContentProviderOperation
            .newDelete(ScheduleContract.addCallerIsSyncAdapterParameter(Announcements.CONTENT_URI)).build());

    StringBuilder sb = new StringBuilder();
    for (Activity activity : activities.getItems()) {
        // Filter out anything not including the conference hashtag.
        sb.setLength(0);
        appendIfNotEmpty(sb, activity.getAnnotation());
        if (activity.getObject() != null) {
            appendIfNotEmpty(sb, activity.getObject().getContent());
        }

        if (!sb.toString().contains(UIUtils.CONFERENCE_HASHTAG)) {
            continue;
        }

        // Insert announcement info
        batch.add(ContentProviderOperation
                .newInsert(ScheduleContract.addCallerIsSyncAdapterParameter(Announcements.CONTENT_URI))
                .withValue(SyncColumns.UPDATED, System.currentTimeMillis())
                .withValue(Announcements.ANNOUNCEMENT_ID, activity.getId())
                .withValue(Announcements.ANNOUNCEMENT_DATE, activity.getUpdated().getValue())
                .withValue(Announcements.ANNOUNCEMENT_TITLE, activity.getTitle())
                .withValue(Announcements.ANNOUNCEMENT_ACTIVITY_JSON, activity.toPrettyString())
                .withValue(Announcements.ANNOUNCEMENT_URL, activity.getUrl()).build());
    }

    return batch;
}

From source file:com.gdgdevfest.android.apps.devfestbcn.sync.SyncHelper.java

License:Apache License

private Googledevelopers getConferenceAPIClient() {
    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new GsonFactory();
    GoogleCredential credential = new GoogleCredential().setAccessToken(AccountUtils.getAuthToken(mContext));
    // Note: The Googledevelopers API is unique, in that it requires an API key in addition to the client
    //       ID normally embedded an an OAuth token. Most apps will use one or the other.
    return new Googledevelopers.Builder(httpTransport, jsonFactory, null)
            .setApplicationName(NetUtils.getUserAgent(mContext))
            .setGoogleClientRequestInitializer(new CommonGoogleClientRequestInitializer(Config.API_KEY))
            .setHttpRequestInitializer(credential).build();
}

From source file:com.google.cloud.genomics.utils.GenomicsFactory.java

License:Apache License

/**
 * Prepare an AbstractGoogleJsonClient.Builder using an API key.
 *
 * @param builder The builder to be prepared.
 * @param apiKey The API key of the Google Cloud Platform project.
 * @return The passed in builder, for easy chaining.
 *//*from   w ww. j av  a2s  .  co m*/
public <T extends AbstractGoogleJsonClient.Builder> T fromApiKey(T builder, String apiKey) {
    Preconditions.checkNotNull(builder);
    Preconditions.checkNotNull(apiKey);
    return prepareBuilder(builder, null, new CommonGoogleClientRequestInitializer(apiKey));
}

From source file:com.google.samples.apps.iosched.io.AnnouncementsFetcher.java

License:Open Source License

public ArrayList<ContentProviderOperation> fetchAndParse() throws IOException {
    final ArrayList<ContentProviderOperation> batch = Lists.newArrayList();

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

    // Set up the main Google+ class
    Plus plus = new Plus.Builder(httpTransport, jsonFactory, null)
            .setApplicationName(NetUtils.getUserAgent(mContext))
            .setGoogleClientRequestInitializer(new CommonGoogleClientRequestInitializer(Config.API_KEY))
            .build();//from  ww w . j  a  va 2 s. co m

    ActivityFeed activities;
    try {
        activities = plus.activities().list(Config.ANNOUNCEMENTS_PLUS_ID, "public").setMaxResults(100l)
                .execute();
        if (activities == null || activities.getItems() == null) {
            throw new IOException("Activities list was null.");
        }

    } catch (IOException e) {
        LOGE(TAG, "Error fetching announcements", e);
        return batch;
    }

    LOGI(TAG, "Updating announcements data");

    // Clear out existing announcements
    batch.add(ContentProviderOperation
            .newDelete(ScheduleContract.addCallerIsSyncAdapterParameter(Announcements.CONTENT_URI)).build());

    StringBuilder sb = new StringBuilder();
    for (Activity activity : activities.getItems()) {
        // Filter out anything not including the conference hashtag.
        sb.setLength(0);
        appendIfNotEmpty(sb, activity.getAnnotation());
        if (activity.getObject() != null) {
            appendIfNotEmpty(sb, activity.getObject().getContent());
        }

        if (!sb.toString().contains(Config.CONFERENCE_HASHTAG)) {
            continue;
        }

        // Insert announcement info
        batch.add(ContentProviderOperation
                .newInsert(ScheduleContract.addCallerIsSyncAdapterParameter(Announcements.CONTENT_URI))
                .withValue(SyncColumns.UPDATED, System.currentTimeMillis())
                .withValue(Announcements.ANNOUNCEMENT_ID, activity.getId())
                .withValue(Announcements.ANNOUNCEMENT_DATE, activity.getUpdated().getValue())
                .withValue(Announcements.ANNOUNCEMENT_TITLE, activity.getTitle())
                .withValue(Announcements.ANNOUNCEMENT_ACTIVITY_JSON, activity.toPrettyString())
                .withValue(Announcements.ANNOUNCEMENT_URL, activity.getUrl()).build());
    }

    return batch;
}

From source file:com.saarang.samples.apps.iosched.io.AnnouncementsFetcher.java

License:Open Source License

public ArrayList<ContentProviderOperation> fetchAndParse() throws IOException {
    final ArrayList<ContentProviderOperation> batch = Lists.newArrayList();

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

    // Set up the main Google+ class
    Plus plus = new Plus.Builder(httpTransport, jsonFactory, null)
            .setApplicationName(NetUtils.getUserAgent(mContext))
            .setGoogleClientRequestInitializer(new CommonGoogleClientRequestInitializer(Config.API_KEY))
            .build();/*from ww  w.j  av a 2  s .  c  o  m*/

    ActivityFeed activities;
    try {
        activities = plus.activities().list(Config.ANNOUNCEMENTS_PLUS_ID, "public").setMaxResults(100l)
                .execute();
        if (activities == null || activities.getItems() == null) {
            throw new IOException("Activities list was null.");
        }

    } catch (IOException e) {
        LogUtils.LOGE(TAG, "Error fetching announcements", e);
        return batch;
    }

    LogUtils.LOGI(TAG, "Updating announcements data");

    // Clear out existing announcements
    batch.add(ContentProviderOperation
            .newDelete(ScheduleContract.addCallerIsSyncAdapterParameter(Announcements.CONTENT_URI)).build());

    StringBuilder sb = new StringBuilder();
    for (Activity activity : activities.getItems()) {
        // Filter out anything not including the conference hashtag.
        sb.setLength(0);
        appendIfNotEmpty(sb, activity.getAnnotation());
        if (activity.getObject() != null) {
            appendIfNotEmpty(sb, activity.getObject().getContent());
        }

        if (!sb.toString().contains(Config.CONFERENCE_HASHTAG)) {
            continue;
        }

        // Insert announcement info
        batch.add(ContentProviderOperation
                .newInsert(ScheduleContract.addCallerIsSyncAdapterParameter(Announcements.CONTENT_URI))
                .withValue(SyncColumns.UPDATED, System.currentTimeMillis())
                .withValue(Announcements.ANNOUNCEMENT_ID, activity.getId())
                .withValue(Announcements.ANNOUNCEMENT_DATE, activity.getUpdated().getValue())
                .withValue(Announcements.ANNOUNCEMENT_TITLE, activity.getTitle())
                .withValue(Announcements.ANNOUNCEMENT_ACTIVITY_JSON, activity.toPrettyString())
                .withValue(Announcements.ANNOUNCEMENT_URL, activity.getUrl()).build());
    }

    return batch;
}

From source file:com.sheepdog.mashmesh.util.GoogleApiUtils.java

License:Apache License

public static Drive getDrive() throws GeneralSecurityException, IOException {
    String apiKey = ApplicationConfiguration.getApiKey();
    return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getServiceCredential())
            .setApplicationName(ApplicationConfiguration.getOAuthApplicationName())
            .setGoogleClientRequestInitializer(new CommonGoogleClientRequestInitializer(apiKey)).build();
}

From source file:data.gCal.java

public static Calendar getCalendar(HttpSession s) {
    try {/*from www .j  av  a 2 s  .c  om*/
        List<String> scopes = new ArrayList<String>();
        scopes.add(CalendarScopes.CALENDAR);
        String apiKey = "AIzaSyCOWx_OFJzinBA1LlqveyHmNz9JkWNNKe0";
        //URL url = getClass().getResource("privatekey.p12");
        GoogleCredential credential = new GoogleCredential.Builder().setJsonFactory(JSON_FACTORY)
                .setTransport(new NetHttpTransport())
                .setServiceAccountId(
                        "964566859240-1k0qlffp2n3j6fr6gv6fthk2l3i81rd8@developer.gserviceaccount.com")
                .setServiceAccountScopes(scopes)
                .setServiceAccountPrivateKeyFromP12File(new File(s.getServletContext()
                        .getRealPath("/WEB-INF/15d1f14007a0cfd9a2ed98f30f4dd6cc8d564232-privatekey.p12")))
                .build();
        GoogleClientRequestInitializer keyInitializer = new CommonGoogleClientRequestInitializer(apiKey);
        service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, null).setHttpRequestInitializer(credential)
                .setGoogleClientRequestInitializer(keyInitializer).build();

    } catch (IOException ex) {
        //Logger.getLogger(CreateEventServlet.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println(ex.toString());
    } catch (GeneralSecurityException ex) {
        //Logger.getLogger(CreateEventServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
    return service;
}

From source file:gplus.to.rss.GplusToRssServlet.java

License:Apache License

/**
 * Initialize the service/*from  ww w  . j  a  v a  2  s. c o  m*/
 */
@Override
public void init() throws ServletException {

    String googleApiKeyParam = this.getServletConfig().getInitParameter(GOOGLE_API_KEY_INIT_PARAM_NAME);
    String[] googleApiKeys = googleApiKeyParam.split(",");

    this.plusClients = new LinkedHashMap<String, Plus>();
    for (String googleApiKey : googleApiKeys) {
        // Initialisation of Google+ Client
        Plus plusClient = new Plus.Builder(new NetHttpTransport(), new JacksonFactory(), null)
                .setApplicationName("gplus-to-rss")
                .setGoogleClientRequestInitializer(new CommonGoogleClientRequestInitializer(googleApiKey))
                .build();
        this.plusClients.put(googleApiKey, plusClient);
    }
}

From source file:org.megam.deccanplato.provider.googleapp.handler.DriveImpl.java

License:Open Source License

/**
 * this method initialize the operations to perform (like upload and download a file)
 * authentication set in this method by calling Drive class's build method
 *///from  w w  w .  j  av  a  2 s  .  c om
@Override
public void setArguments(BusinessActivityInfo tempBizInfo, Map<String, String> tempArgs) {
    this.bizInfo = tempBizInfo;
    this.args = tempArgs;
    final GoogleClientRequestInitializer KEY_INITIALIZER = new CommonGoogleClientRequestInitializer(
            args.get(API_KEY));
    drive = new Drive.Builder(new NetHttpTransport(), new JacksonFactory(), null)
            .setApplicationName(args.get(APPLICATION_NAME)).setGoogleClientRequestInitializer(KEY_INITIALIZER)
            .build();

}