4
votes

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?.

1
I found an example using the Google books api. github.com/google/google-api-java-client-samples/blob/master/… I am not a java dev you can probably pick apart the code better then I can - DaImTo
@DaImTo Thank you very much. I got the idea and now I am able to access public data. - J. Aftab
If you got it to work I suggest you answer your own question supply enough to code to help someone else. You will get points for answering the question when you accept it. I agree it was hard to find that code. The public calls are not well documented for Java. - DaImTo
@DaImTo Yeah you are right, it is not well documented and it took me three four consecutive days. I will definitely answer. Thanks again - J. Aftab

1 Answers

3
votes

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.