Use Gmail API in a keyword

I use this tutorial to connect to Gmail API: Guida rapida di Java  |  Gmail  |  Google for Developers
I get following error for InputStream:

Groovy:expecting ‘}’, found ‘in’

private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
		// Load client secrets.
		InputStream in = GmailQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
		if (in == null) {
			throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
		}
		GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

		// Build flow and trigger user authorization request.
		GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
		HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
		.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
		.setAccessType("offline")
		.build();
		LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
		return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
	}

Groovy language reserves a word in.

See
https://docs.oracle.com/cloud/latest/big-data-discovery-cloud/BDDDE/rsu_transform_unsupported_features.htm#BDDDE-reference_B4D419B4A7634CDD8E312D3AF68F8277

You should not use in as a variable name.

1 Like

@kazurayam : Thanks it is resolved, but I got error during execution:

Caused by: java.lang.NoSuchMethodError: com.google.api.client.http.HttpRequest.setResponseReturnRawInputStream(Z)Lcom/google/api/client/http/HttpRequest;

I had a look at the Google’s document. The document assumes that the project’s CLASSPATH is configured by Gradle when you do:

$ gradle init --type basic

On the other hand, Katalon Studio has its own .classpath setting which does NOT include the external dependencies (*.jar files) resolved by Gradle. Therefore Groovy compiler in KS can not refer to the jar file of Gmail API downloaded by Gradle in your <HOME>/.m2 directory. This is the reason why you got the error:

Caused by: java.lang.NoSuchMethodError: com.google.api.client.http.HttpRequest.setResponseReturnRawInputStream(Z)Lcom/google/api/client/http/HttpRequest;

Katalon Studio does not allow users to make any ad hoc changes to the .classpath.

You would never be able to run the Gmail tutorial sample in Katalon Studio.

1 Like

You can study the sample as instructed in other environments, such as

  1. Eclipse with Gradle plugin
  2. Intelli-j IDEA
  3. Visual Studio Code
  4. traditional editors (vi, emacs, Atom) + Gradle

Do you want to write a Keyword Groovy class which depends on the Gmail API library? Yes, you can do it. In KS, you are just asked to follow Katalon’s way.

  1. download the Gmail API’s jar and locate it in the <projectDir>/Drivers directory as described here
  2. write Groovy codes in <project/Dir>/Keywords/scripts/groovy/yourpackagename/Classname.groovy
  3. Supprisingly you can write Java codes in <project/Dir>/Keywords/scripts/groovy/yourpackagename/Classname.java
  4. How to run your class? how to do unit-testings for your classes in KS? … there would be a lot more for you to find out.

By the way, why not you try the Gmail Message plugin available at Katalon Store?

https://store.katalon.com/product/39/Read-and-Write-Gmail-Messages-Keywords

I have never used it, so have no say about it.

1 Like

@kazurayam : This plugin: Read and Write Gmail Messages Keywords seems obsolete, it isn’t working at this time, throw error message.
I would like to make a class, which depends on Gmail API. JAR files are added, project is running and browser window is opened to get token. After successful authorization I got error message, which I pasted earlier.

Following jars are already imported:

commons-codec-1.10.jar
commons-logging-1.2.jar
google-api-client-1.31.3.jar
google-api-client-extensions-1.6.0-beta.jar
google-api-client-jackson2-1.31.3.jar
google-api-client-java6-1.31.3.jar
google-api-services-gmail-v1-rev110-1.25.0.jar
google-http-client-1.27.0.jar
google-http-client-jackson2-1.27.0.jar
google-oauth-client-java6-1.31.4.jar
google-oauth-client-jetty-1.31.4.jar
guava-20.0.jar
httpclient-4.5.5.jar
httpcore-4.4.9.jar
j2objc-annotations-1.1.jar
jackson-core-2.9.6.jar
jsr305-3.0.2.jar

I have no experience about Gmail API. I am unable to help you, sorry.

Similar issue

This issue seems to be newly found in Feb 2021. I guess this issue is due to some problem in the recent version 1.31.3 of Google’s library which you tried.

I found in https://mvnrepository.com/artifact/com.google.api-client/google-api-client, there are older versions are still available. You could try an older version like 1.30.10. Possibly you could avoid the hidden problem in the latest version.

1 Like

I got that exception for that line:
ListLabelsResponse listResponse = service.users().labels().list(user).execute();

I updated dependencies. List:

commons-codec-1.15.jar
commons-logging-1.2.jar
google-api-client-1.31.3.jar
google-api-client-extensions-1.6.0-beta.jar
google-api-client-jackson2-1.31.3.jar
google-api-client-java6-1.31.3.jar
google-api-services-gmail-v1-rev110-1.25.0.jar
google-http-client-1.39.1.jar
google-http-client-jackson2-1.39.1.jar
google-oauth-client-java6-1.31.4.jar
google-oauth-client-jetty-1.31.4.jar
guava-30.1.1-jre.jar
httpclient-4.5.13.jar
httpcore-4.4.14.jar
j2objc-annotations-1.3.jar
jackson-core-2.12.2.jar
jsr305-3.0.2.jar