Claus Ibsen created CAMEL-27187:
-----------------------------------
Summary: camel-google - OAuth login fails when scopes are set
(calendar-stream always)
Key: CAMEL-27187
URL: https://issues.apache.org/jira/browse/CAMEL-27187
Project: Camel
Issue Type: Bug
Components: camel-google
Reporter: Claus Ibsen
h3. What happens
When a Google Workspace component logs in with OAuth ({{clientId}},
{{clientSecret}}, {{refreshToken}} / {{accessToken}}) and {{scopes}} is set,
creating the client fails with a bare {{IllegalArgumentException}} from the
Google client library.
* *google-calendar-stream* cannot log in with OAuth at all:
{{GoogleCalendarStreamConfiguration.scopes}} defaults to
{{CalendarScopes.CALENDAR}}, so the scopes are never empty. Every poll fails
with {{Could not create Google Calendar client}}.
* *google-mail*, *google-mail-stream*, *google-sheets*, *google-sheets-stream*,
*google-calendar* and *google-drive* fail the same way as soon as the user sets
{{scopes}} together with OAuth (without {{scopes}} their {{getScopesAsList()}}
returns null and OAuth works).
The Kamelet {{google-calendar-source}} therefore cannot use its OAuth options;
only a service account login works. The Gmail and Sheets Kamelets have to tell
users to leave {{scopes}} unset with OAuth.
h3. How to reproduce
{code}
from("google-calendar-stream://events?clientId=xxx&clientSecret=xxx&refreshToken=xxx&accessToken=xxx&applicationName=test/1.0&calendarId=primary")
.log("${body}");
{code}
{noformat}
WARN GoogleCalendarStreamConsumer : Failed polling endpoint:
google-calendar-stream://events?... Will try again at next poll. Caused by:
[org.apache.camel.RuntimeCamelException - Could not create Google Calendar
client.]
org.apache.camel.RuntimeCamelException: Could not create Google Calendar client.
at
org.apache.camel.component.google.calendar.BatchGoogleCalendarClientFactory.makeClient(BatchGoogleCalendarClientFactory.java:73)
...
Caused by: java.lang.IllegalArgumentException
at
com.google.common.base.Preconditions.checkArgument(Preconditions.java:127)
at
com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:35)
at
com.google.api.client.googleapis.auth.oauth2.GoogleCredential.<init>(GoogleCredential.java:337)
at
com.google.api.client.googleapis.auth.oauth2.GoogleCredential$Builder.build(GoogleCredential.java:581)
at
org.apache.camel.component.google.common.GoogleCredentialsHelper.createOAuthCredential(GoogleCredentialsHelper.java:225)
at
org.apache.camel.component.google.common.GoogleCredentialsHelper.getOAuthCredential(GoogleCredentialsHelper.java:166)
at
org.apache.camel.component.google.calendar.BatchGoogleCalendarClientFactory.makeClient(BatchGoogleCalendarClientFactory.java:69)
{noformat}
The same stack (via {{BatchGoogleMailClientFactory.makeClient}} /
{{BatchGoogleSheetsClientFactory}}) is seen for {{google-mail-stream}} and
{{google-sheets-stream}} with OAuth plus
{{scopes=https://www.googleapis.com/auth/gmail.modify}} (or a Sheets scope).
h3. Where
{{components/camel-google/camel-google-common/src/main/java/org/apache/camel/component/google/common/GoogleCredentialsHelper.java}},
{{createOAuthCredential}}:
{code:java}
Credential credential = new GoogleCredential.Builder()
.setJsonFactory(jsonFactory)
.setTransport(transport)
.setClientSecrets(config.getClientId(), config.getClientSecret())
.setServiceAccountScopes(scopes)
.build();
{code}
{{GoogleCredential}} only accepts service account scopes together with a
service account private key; for a credential without one it checks that the
service account id, scopes and user are all null, hence the
{{IllegalArgumentException}}.
h3. Suggested fix
Do not call {{setServiceAccountScopes}} in {{createOAuthCredential}}. With
OAuth the scopes are fixed when the refresh token is obtained (the consent),
not when the access token is refreshed, so the {{scopes}} option has nothing to
do there. Add a test per component that builds the client with OAuth options
and {{scopes}} set (and one for {{google-calendar-stream}} with its default
scopes).
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)