# Use Gmail API in a keyword

**URL:** <https://forum.katalon.com/t/use-gmail-api-in-a-keyword/53226>\
**Category:** Miscellaneous\
**Tags:** start-page\
**Created:** [March 23, 2021, 12:32pm UTC](https://forum.katalon.com/t/use-gmail-api-in-a-keyword/53226 "2021-03-23T12:32:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![plaidshirtakos](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/plaidshirtakos/32/9361_2.png) [@plaidshirtakos](https://forum.katalon.com/u/plaidshirtakos)\
**Post date:** [March 23, 2021, 12:32pm UTC](https://forum.katalon.com/t/use-gmail-api-in-a-keyword/53226/1 "2021-03-23T12:32:40Z")

</div>

I use this tutorial to connect to Gmail API: [Guida rapida di Java &nbsp;|&nbsp; Gmail &nbsp;|&nbsp; Google for Developers](https://developers.google.com/gmail/api/quickstart/java)  
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");
	}

```

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [March 23, 2021, 2:42pm UTC](https://forum.katalon.com/t/use-gmail-api-in-a-keyword/53226/2 "2021-03-23T14:42:39Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![plaidshirtakos](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/plaidshirtakos/32/9361_2.png) [@plaidshirtakos](https://forum.katalon.com/u/plaidshirtakos)\
**Post date:** [March 23, 2021, 6:28pm UTC](https://forum.katalon.com/t/use-gmail-api-in-a-keyword/53226/4 "2021-03-23T18:28:54Z")

</div>

@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;

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [March 23, 2021, 11:37pm UTC](https://forum.katalon.com/t/use-gmail-api-in-a-keyword/53226/5 "2021-03-23T23:37:26Z")

</div>

I had a look at [the Google’s document](https://developers.google.com/gmail/api/quickstart/java). The document assumes that the project’s CLASSPATH is configured by Gradle when you do:

```auto
$ 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](https://developers.google.com/gmail/api/quickstart/java) in Katalon Studio.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [March 23, 2021, 11:40pm UTC](https://forum.katalon.com/t/use-gmail-api-in-a-keyword/53226/6 "2021-03-23T23:40:31Z")

</div>

You can study [the sample](https://developers.google.com/gmail/api/quickstart/java) as instructed in other environments, such as

1. Eclipse with Gradle plugin
2. Intelli-j IDEA
3. [Visual Studio Code](https://code.visualstudio.com/docs/languages/java)
4. traditional editors (vi, emacs, Atom) + Gradle

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [March 23, 2021, 11:51pm UTC](https://forum.katalon.com/t/use-gmail-api-in-a-keyword/53226/7 "2021-03-23T23:51:08Z")

</div>

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](https://docs.katalon.com/katalon-studio/docs/external-libraries.html)
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](https://store.katalon.com/product/39/Read-and-Write-Gmail-Messages-Keywords)

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

---

<div class="post-metadata">

**Author:** ![plaidshirtakos](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/plaidshirtakos/32/9361_2.png) [@plaidshirtakos](https://forum.katalon.com/u/plaidshirtakos)\
**Post date:** [March 24, 2021, 7:35am UTC](https://forum.katalon.com/t/use-gmail-api-in-a-keyword/53226/8 "2021-03-24T07:35:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![plaidshirtakos](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/plaidshirtakos/32/9361_2.png) [@plaidshirtakos](https://forum.katalon.com/u/plaidshirtakos)\
**Post date:** [March 24, 2021, 7:48am UTC](https://forum.katalon.com/t/use-gmail-api-in-a-keyword/53226/9 "2021-03-24T07:48:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [March 24, 2021, 8:28am UTC](https://forum.katalon.com/t/use-gmail-api-in-a-keyword/53226/10 "2021-03-24T08:28:41Z")

</div>

> [@plaidshirtakos](#):
>
> After successful authorization I got error message, which I pasted earlier.

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

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [March 24, 2021, 8:35am UTC](https://forum.katalon.com/t/use-gmail-api-in-a-keyword/53226/11 "2021-03-24T08:35:48Z")

</div>

Similar issue

> <https://stackoverflow.com/questions/66369621/facing-nosuchmethoderror-for-httprequest-setresponsereturnrawinputstream>

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](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.

---

<div class="post-metadata">

**Author:** ![plaidshirtakos](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/plaidshirtakos/32/9361_2.png) [@plaidshirtakos](https://forum.katalon.com/u/plaidshirtakos)\
**Post date:** [March 24, 2021, 12:52pm UTC](https://forum.katalon.com/t/use-gmail-api-in-a-keyword/53226/12 "2021-03-24T12:52:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![plaidshirtakos](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/plaidshirtakos/32/9361_2.png) [@plaidshirtakos](https://forum.katalon.com/u/plaidshirtakos)\
**Post date:** [March 25, 2021, 11:50am UTC](https://forum.katalon.com/t/use-gmail-api-in-a-keyword/53226/13 "2021-03-25T11:50:31Z")

</div>

I updated dependencies. List:

```auto
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

```
