Search code examples
javagoogle-apigoogle-plusdesktop-applicationrestful-authentication

using an api key for google+ to make request from a desktop application in java


I am developing an application using restful APIs of Twitter and Google to access public data on these social platforms.

According to my initial research, accessing public data can be achievable through an API key for Google APIs, however the problem is that I cannot find any method or example for making request to a Google API in their extensive Google API Client Library for Java.

Is there any way I can access public feeds through API key only by using google client library for java?

In twitter you can request bearer token for application and make api requests. I want know that can I achieve this google as well? if yes then how?.


Solution

  • Unfortunately, Google Client library for Java is not well documented in terms of accessing public feeds simply through an API key but it can be achievable. After an extensive research and thanks to @DalmTo, I have found the answer to my question.

    Before starting I would suggest to go through library's JAVADOC reference to understand. See here

    First we need to establish an API connection so application can make API calls like GET,SEARCH but not POST (because it needs authorization). First it needs some initializers.

    private static final String API_KEY="ENTER_YOUR_KEY_HERE";
    private static HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    private static JsonFactory jsonFactory = new JacksonFactory.getDefaultInstance();
    

    Now using these initializers to build a plus object which later makes the API calls.

    public static Plus plus = new Plus.builder(httpTransport,jsonFactory,null).setApplicationName("your app name").setGoogleClientRequestInitializer(new PlusRequestInitializer(API_KEY))
                    .build();
    //It is better to set the application name otherwise it gives warning.
    //now run commands here or call your methods, whatever your approach is. To know how make calls
    //see the link attached.
    

    To know how make API calls click here

    It has worked for me. I hope it will surely work for you guys. I will try to upload the complete source code on GitHub.