I'm trying to run the example code given by google and the following error appears:
"message" : "The request is missing a valid API key.",
"status" : "PERMISSION_DENIED"
I created a credential and own client id, but I do not know where to insert in the code
código de exemplo:
package javaapplication1;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.model.ValueRange;
import java.io.IOException;
import java.security.GeneralSecurityException;
public class JavaApplication1 {
public static void main(String args[]) throws IOException, GeneralSecurityException {
// The ID of the spreadsheet to retrieve data from.
String spreadsheetId = "1er12_Tp-m1IJNRfz-72VwVY14Gj10b-IexY60fzZiGg"; // TODO: Update placeholder value.
// The A1 notation of the values to retrieve.
String range = "a1:b4"; // TODO: Update placeholder value.
// How values should be represented in the output.
// The default render option is ValueRenderOption.FORMATTED_VALUE.
String valueRenderOption = ""; // TODO: Update placeholder value.
// How dates, times, and durations should be represented in the output.
// This is ignored if value_render_option is
// FORMATTED_VALUE.
// The default dateTime render option is [DateTimeRenderOption.SERIAL_NUMBER].
String dateTimeRenderOption = ""; // TODO: Update placeholder value.
Sheets sheetsService = createSheetsService();
Sheets.Spreadsheets.Values.Get request =
sheetsService.spreadsheets().values().get(spreadsheetId, range);
request.setValueRenderOption(valueRenderOption);
request.setDateTimeRenderOption(dateTimeRenderOption);
ValueRange response = request.execute();
// TODO: Change code below to process the `response` object:
System.out.println(response);
}
public static Sheets createSheetsService() throws IOException, GeneralSecurityException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
// TODO: Change placeholder below to generate authentication credentials. See
// https://developers.google.com/sheets/quickstart/java#step_3_set_up_the_sample
//
// Authorize using one of the following scopes:
// "https://www.googleapis.com/auth/drive"
// "https://www.googleapis.com/auth/drive.file"
// "https://www.googleapis.com/auth/drive.readonly"
// "https://www.googleapis.com/auth/spreadsheets"
// "https://www.googleapis.com/auth/spreadsheets.readonly"
static Urlshortener newUrlshortener() {
AppIdentityCredential credential =
new AppIdentityCredential(Arrays.asList(UrlshortenerScopes.URLSHORTENER));
return new Urlshortener.Builder(new UrlFetchTransport(), JacksonFactory.getDefaultInstance(), credential)
.build();
}
return new Sheets.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("Google-SheetsSample/0.1")
.build();
}
}
the code is an example given by google, and I'm still learning, my only need is to read a spreadsheet from the google spreadsheet that has been filled up with forms answers from google
THanks
Related
I am trying to generate a list of videos from a channel that I am subscribed to.
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.PlaylistItemListResponse;
import com.google.api.services.youtube.model.SubscriptionListResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collection;
import java.util.Collections;
public class ApiExample {
private static final String CLIENT_SECRETS= "client_secret.json";
private static final Collection<String> SCOPES =
Collections.singletonList("https://www.googleapis.com/auth/youtube.readonly");
private static final String APPLICATION_NAME = "Stack Overflow MRE";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
/**
* Create an authorized Credential object.
*
* #return an authorized Credential object.
* #throws IOException in the event that the client_secrets.json file is not found
*/
public static Credential authorize(final NetHttpTransport httpTransport) throws IOException {
// Load client secrets.
InputStream in = ApiExample.class.getResourceAsStream(CLIENT_SECRETS);
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
.build();
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
/**
* Build and return an authorized API client service.
*
* #return an authorized API client service
* #throws GeneralSecurityException, IOException
*/
public static YouTube getService() throws GeneralSecurityException, IOException {
final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Credential credential = authorize(httpTransport);
return new YouTube.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
/**
* Call function to create API service object. Define and
* execute API request. Print API response.
*
* #throws GeneralSecurityException, IOException, GoogleJsonResponseException
*/
public static void main(String[] args)
throws GeneralSecurityException, IOException {
YouTube youtubeService = getService();
// Define and execute the API request
YouTube.Subscriptions.List request = youtubeService.subscriptions()
.list("snippet");
SubscriptionListResponse response = request.setMine(true).setMaxResults(1L).execute();
String channelId = response.getItems().get(0).getSnippet().getResourceId().getChannelId();
YouTube.PlaylistItems.List playListRequest = youtubeService.playlistItems().list("snippet");
PlaylistItemListResponse playlistResponse = playListRequest.setPlaylistId(channelId).execute();
playlistResponse.getItems().forEach(System.out::println);
}
}
I have read YouTube API to fetch all videos on a channel, however my question is a little different because I am trying to get the list of videos from a Subscription, rather than from a Channel.
I attempted to isolate the Channel ID in order to follow the instructions in the video from Google Developers about how to do this. I used String channelId = response.getItems().get(0).getSnippet().getResourceId().getChannelId();
However, when I run the above code, which should print out a list of videos from that Channel in JSON notation, I see this error instead:
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
{
"code" : 404,
"errors" : [ {
"domain" : "youtube.playlistItem",
"location" : "playlistId",
"locationType" : "parameter",
"message" : "The playlist identified with the request's <code>playlistId</code> parameter cannot be found.",
"reason" : "playlistNotFound"
} ],
"message" : "The playlist identified with the request's <code>playlistId</code> parameter cannot be found."
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:150)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1067)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at ApiExample.main(ApiExample.java:93)
Evidently, I have not correctly isolated the Channel ID, or else I have misunderstood the video. What is the correct way to get the videos from a YouTube subscription?
Edit
The following change in my main method worked:
public static void main(String[] args)
throws GeneralSecurityException, IOException {
YouTube youtubeService = getService();
// Define and execute the API request
YouTube.Subscriptions.List request = youtubeService.subscriptions()
.list("snippet")
SubscriptionListResponse response = request.setMine(true).setMaxResults(1L).execute();
String channelId = response.getItems().get(0).getSnippet().getResourceId().getChannelId();
YouTube.Channels.List channelRequest = youtubeService.channels().list("contentDetails");
ChannelListResponse channelResponse = channelRequest.setId(channelId).execute();
String playListID = channelResponse.getItems().get(0).getContentDetails().getRelatedPlaylists().getUploads();
YouTube.PlaylistItems.List playListRequest = youtubeService.playlistItems().list("snippet");
PlaylistItemListResponse playlistResponse = playListRequest.setPlaylistId(playListID).execute();
playlistResponse.getItems().forEach(System.out::println);
}
The problem with your code above boils down to the following doc entry:
playlistId (string)
The playlistId parameter specifies the unique ID of the playlist for which you want to retrieve playlist items. Note that even though this is an optional parameter, every request to retrieve playlist items must specify a value for either the id parameter or the playlistId parameter.
Things should come into light by now: playlistId is the ID of a playlist, hence cannot be the ID of a channel.
But for listing all uploaded videos of a given channel (identified by its ID), one has to do the following:
Invoke the PlaylistItems.list API endpoint queried with the parameter playlistId set to the ID of that channel's uploads playlist.
This latter ID may very easily be obtained by invoking the Channels.list endpoint queried with the parameter id set to your channel's ID.
The uploads playlist ID is then to be found within the endpoint's JSON response as value of the property:
items[0].contentDetails.relatedPlaylists.uploads.
Translated to Java, this property path would become the following chain of getters, ending with getUploads:
.getItems().get(0).getContentDetails().getRelatedPlaylists().getUploads().
Note that for a given channel, you need to obtain the uploads playlist ID only once, then use it as many times as you wish.
Usually, a channel ID and its corresponding uploads playlist ID are related by s/^UC([0-9a-zA-Z_-]{22})$/UU\1/.
I'm creating a chatbot for a java application with Watson Assistant, the servlet code:
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String sessionIdOut = "";
String question = req.getParameter("message");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// Set up Assistant service.
IamOptions iamOptions = new IamOptions.Builder().apiKey("<apikey>").build();
Assistant service = new Assistant("2018-09-20", iamOptions);
service.setEndPoint("https://gateway-lon.watsonplatform.net/assistant/api/");
assistantId = "<assistantid>";
// Create session.
CreateSessionOptions createSessionOptions = new CreateSessionOptions.Builder(assistantId).build();
SessionResponse session = service.createSession(createSessionOptions).execute();
sessionId = session.getSessionId();
// Suppress log messages in stdout.
LogManager.getLogManager().reset();
// Initialize with an empty value to start the conversation.
String inputText = question;
// Send message to assistant.
MessageInput input = new MessageInput.Builder().text(inputText).build();
MessageOptions messageOptions = new MessageOptions.Builder(assistantId, sessionId)
.input(input)
.build();
MessageResponse response = service.message(messageOptions).execute();
// Print the output from the dialog if any. Assumes a single text response.
List<DialogRuntimeResponseGeneric> responseGeneric = response.getOutput().getGeneric();
if(responseGeneric.size() > 0) {
System.out.println(response.getOutput()/*.getGeneric().get(0).getText()*/);
String answer = response.getOutput().getGeneric().get(0).getText();
// set up the response
res.setContentType("text/html");
res.setHeader("Cache-Control", "no-cache");
// write out the response string
res.getWriter( ).write(answer);
}
// Prompt for next round of input.
System.out.print(">> ");
}
Currently, the servlet always creates a new session and sets up the assistant when the GET request arrives from the user interface. I want it to create a new session and set up assistant service only once when the server starts.
Tryed to solve the problem by adding init() function, and writing the session creation and assistant setup code inside that init() function like this:
#Override
public void init() throws ServletException {
// Set up Assistant service.
IamOptions iamOptions = new IamOptions.Builder().apiKey("<apikey>").build();
Assistant service = new Assistant("2018-09-20", iamOptions);
service.setEndPoint("https://gateway-lon.watsonplatform.net/assistant/api/");
assistantId = "<assistantid>";
// Create session.
CreateSessionOptions createSessionOptions = new CreateSessionOptions.Builder(assistantId).build();
SessionResponse session = service.createSession(createSessionOptions).execute();
sessionId = session.getSessionId();
super.init();
}
But it doesn't work, when I write a question in user interface, it sends me back 500 status code.
I solved the problem!
Working code looks like:
package com.jtypebot;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.ibm.watson.developer_cloud.assistant.v2.Assistant;
import com.ibm.watson.developer_cloud.assistant.v2.model.CreateSessionOptions;
import com.ibm.watson.developer_cloud.assistant.v2.model.DeleteSessionOptions;
import com.ibm.watson.developer_cloud.assistant.v2.model.DialogRuntimeResponseGeneric;
import com.ibm.watson.developer_cloud.assistant.v2.model.MessageInput;
import com.ibm.watson.developer_cloud.assistant.v2.model.MessageOptions;
import com.ibm.watson.developer_cloud.assistant.v2.model.MessageResponse;
import com.ibm.watson.developer_cloud.assistant.v2.model.RuntimeIntent;
import com.ibm.watson.developer_cloud.assistant.v2.model.SessionResponse;
import com.ibm.watson.developer_cloud.service.security.IamOptions;
import java.util.List;
import java.util.logging.LogManager;
#WebServlet("/JtypeBot")
public class JtypeBot extends HttpServlet {
private static final long serialVersionUID = 1L;
String sessionId;
String assistantId;
Assistant service;
/**
* #see HttpServlet#HttpServlet()
*/
public JtypeBot() {
super();
}
#Override
public void init() throws ServletException {
super.init();
// Set up Assistant service.
IamOptions iamOptions = new IamOptions.Builder().apiKey("<apiKey>").build();
service = new Assistant("2018-09-20", iamOptions);
service.setEndPoint("https://gateway-lon.watsonplatform.net/assistant/api/");
assistantId = "<assistantId>"; // replace with assistant ID
// Create session.
CreateSessionOptions createSessionOptions = new CreateSessionOptions.Builder(assistantId).build();
SessionResponse session = service.createSession(createSessionOptions).execute();
sessionId = session.getSessionId();
System.out.print(sessionId);
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String sessionIdOut = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String question = req.getParameter("message");
// Suppress log messages in stdout.
LogManager.getLogManager().reset();
// Initialize with empty value to start the conversation.
String inputText = question;
// Send message to assistant.
MessageInput input = new MessageInput.Builder().text(inputText).build();
MessageOptions messageOptions = new MessageOptions.Builder(assistantId, sessionId)
.input(input)
.build();
MessageResponse response = service.message(messageOptions).execute();
// Print the output from dialog, if any. Assumes a single text response.
List<DialogRuntimeResponseGeneric> responseGeneric = response.getOutput().getGeneric();
if(responseGeneric.size() > 0) {
System.out.println(response.getOutput()/*.getGeneric().get(0).getText()*/);
String answer = response.getOutput().getGeneric().get(0).getText();
// set up the response
res.setContentType("text/html");
res.setHeader("Cache-Control", "no-cache");
// write out the response string
res.getWriter( ).write(answer);
}
// Prompt for next round of input.
System.out.print(">> ");
}
}
I'm trying to get the list of Buckets in my Google cloud project using the service account.
Here is the program:
package com.mypackage.api;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.StorageScopes;
import com.google.api.services.storage.model.Bucket;
public class TestClass {
public static void main(String args[]) throws IOException, GeneralSecurityException {
String project = "my-project";
Storage storage = createStorageService();
List<String> list = new ArrayList<String>();
List<Bucket> buckets = storage.buckets().list(project).execute().getItems();
if(buckets != null) {
for(Bucket b : buckets) {
list.add(b.getName());
}
}
}
public static Storage createStorageService() throws IOException, GeneralSecurityException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential credential = GoogleCredential.getApplicationDefault();
if (credential.createScopedRequired()) {
credential = credential.createScoped(Arrays.asList(StorageScopes.DEVSTORAGE_FULL_CONTROL, "https://www.googleapis.com/auth/cloud-platform"));
}
return new Storage.Builder(httpTransport, jsonFactory,
credential).setApplicationName("My Application")
.build();
}
}
And I got the below error:
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "mytool#my-project.iam.gserviceaccount.com does not have storage.buckets.list access to project 333XXXXX67.",
"reason" : "forbidden"
} ],
"message" : "mytool#my-project.iam.gserviceaccount.com does not have storage.buckets.list access to project 333XXXXX67."
}
I have added "Owner" scope to my service account. But still I'm getting permission denied error.
Any help is appreciated.
Check your credential. Are you sure that credential.createScopedRequired() is true, and credentional get required scopes for read info? For example https://www.googleapis.com/auth/devstorage.read_only. Such an answer (403) means that you do not have permission.
I rectified this error by changing the credentials type file. Initially, i took web application file so that's why I got this error. Thank you for responding to my question.
I'm using Floodlight REST API in order to monitor a created virtual network in mininet. My goal is to display an arraylist of all the switches, hosts and statistics for the switches on a web browser using Apache Tomcat web server and HTTP Servlet. The application successfully displays all the switches and hosts, but fails when I'm adding the statistics for the switches.
When I'm mapping JSON string to java objects, the server returns the error in this line:
ArrayList<Switch> queues = mapper.readValue(queueJson, new TypeReference<ArrayList<Switch>>() {
});
The error is:
HTTP status 500 - can not deserialize instance of java.util.arraylist out of start_object token
I have testet it without the switch statistics (Queues) part (with only hosts and devices) and everything works fine, but when I'm adding the queues ArrayList, it returns the above mentioned error.
How can I solve this issue ?. My code is shown below. Thanks in advance
package core;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.restlet.data.MediaType;
import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;
import pojos.Device;
import pojos.Switch;
#WebServlet("/PrintInfo")
public class PrintInfo extends HttpServlet {
private static final long serialVersionUID = 1L;
public PrintInfo() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// create ClientResource object
// List at the switches in the network
ClientResource cResourceSwitches = new ClientResource("http://127.0.0.1:8080/wm/core/controller/switches/json");
StringWriter sWriterSwitches = new StringWriter();
// List all the devices (hosts) in the network
ClientResource cResourceDevices = new ClientResource("http://127.0.0.1:8080/wm/device/");
StringWriter sWriterDevices = new StringWriter();
// List the statistics of the switches in the network
ClientResource cResourceQueues = new ClientResource("http://127.0.0.1:8080/wm/core/switch/all/queue/json");
StringWriter sWriterQueues = new StringWriter();
// get JSON data about switches; the data is put in a string writer
try {
// Getting data from Floodlight as a JSON string
cResourceSwitches.get(MediaType.APPLICATION_JSON).write(sWriterSwitches);
cResourceDevices.get(MediaType.APPLICATION_JSON).write(sWriterDevices);
cResourceQueues.get(MediaType.APPLICATION_JSON).write(sWriterQueues);
} catch (ResourceException e) {
request.setAttribute("error", "Connection with FLoodLight failed!");
request.getRequestDispatcher("WEB-INF/connectionError.jsp").forward(request, response);
return;
}
// put data from string writer into a string object
String switchesJson = sWriterSwitches.toString();
String devicesJson = sWriterDevices.toString();
String queueJson = sWriterQueues.toString();
// map JSON data to Java objects
// ObjectMapper converts between JSON - Java
ObjectMapper mapper = new ObjectMapper();
ArrayList<Switch> switches = mapper.readValue(switchesJson, new TypeReference<ArrayList<Switch>>() {
});
ArrayList<Device> devices = mapper.readValue(devicesJson, new TypeReference<ArrayList<Device>>() {
});
ArrayList<Switch> queues = mapper.readValue(queueJson, new TypeReference<ArrayList<Switch>>() {
});
// put objects in the request so we can use them later in the JSP
request.setAttribute("switches", switches);
request.setAttribute("devices", devices);
request.setAttribute("queues", queues);
// redirect to the jsp
request.getRequestDispatcher("WEB-INF/showInfo.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
}
}
Solved. The Switch class in "ArrayList" can't be used for switch statistics. A new class has to be implemented, which returns the values in
http://127.0.0.1:8080/wm/core/switch/all/queue/json
URI.
I want to use google task api and want to get tasklist,update,delete,add etc.. and I found this link https://developers.google.com/google-apps/tasks/oauth-and-tasks-on-android where step by step procedure is given on that link the library which are given are deprecated.
That's why I have downloaded latetst library google-api-java-client-1.12.0-beta from here http://code.google.com/p/google-api-java-client/downloads/detail?name=google-api-java-client-1.12.0-beta.zip&can=2&q= and google-api-services-tasks-v1-rev5-java-1.12.0-beta from here http://code.google.com/p/google-api-java-client/wiki/APIs#Tasks_API and try the code given and similar to it but no luck not get anything i am successfully get accesstoken but not get anything and in the latest libs most of method are changes so how to inialize the Tasks and get TaskList,create,delete etc...... Not a single document i found related to updated library.
Hope for your regards.
Thanks.
This solution is for Server to server communication using OAuth 2.0
It is a three step process
Authenticate using OAuth 2.0
Get the com.google.api.services.tasks.Tasks service object
Get the required Task or TaskList
In this sample code it uses the domain id "abc.com" and the user is "user1#abc.com". For gmail users, please provide the gmailid (abc#gmail.com) as consumerkey and leave "xoauth_requestor_id" as gmailid
import com.google.api.client.http.*;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.tasks.*;
import com.google.api.client.auth.oauth.OAuthHmacSigner;
import com.google.api.client.auth.oauth.OAuthParameters;
public class GoogleConnection {
public Tasks setup() throws Exception {
com.google.api.services.tasks.Tasks tasks = null;
HttpRequestFactory httpRequestFactory = null;
HttpRequestInitializer httpRequestInitializer = null;
OAuthHmacSigner signer = new OAuthHmacSigner();
HttpTransport httpTransport = new NetHttpTransport();
OAuthParameters oauthParameters = new OAuthParameters();
final ArrayMap<String, Object> customKeys = new ArrayMap<String, Object>();
customKeys.add("xoauth_requestor_id", "user1#abc.com");
signer.clientSharedSecret = "secret_key_received_from_google";
oauthParameters.version = "2.0";
oauthParameters.consumerKey = "abc.com";
oauthParameters.signer = signer;
httpRequestFactory = createRequestFactory(httpTransport, oauthParameters, "20000", "20000");
httpRequestInitializer = httpRequestFactory.getInitializer();
tasks = new com.google.api.services.tasks.Tasks.Builder(httpTransport, new JacksonFactory(), httpRequestInitializer)
.setTasksRequestInitializer(new TasksRequestInitializer() {
#Override
public void initializeTasksRequest(TasksRequest<?> request) throws IOException {
#SuppressWarnings("rawtypes")
TasksRequest tasksRequest = (TasksRequest) request;
tasksRequest.setUnknownKeys(customKeys);
tasksRequest.setKey("keyapi_received_from_google_by_registering_your_app");
}
})
.setApplicationName("")
.build();
return tasks;
}
}
Getting Tasks from a Task List
Instantiate GoogleConnection class
public List<com.google.api.services.tasks.model.Task> getTasksFromTaskList(String taskListId) throws Exception {
com.google.api.services.tasks.Tasks tasksService = googleConnection.setup();
com.google.api.services.tasks.model.Tasks result = tasksService .tasks().list(taskListId).execute();
return result.getItems();
}