[ 
https://issues.apache.org/jira/browse/CAMEL-27187?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18148198#comment-18148198
 ] 

Guillaume Nodet commented on CAMEL-27187:
-----------------------------------------

This issue is being investigated by a coding agent (on behalf of gnodet).

Triage analysis: Bug confirmed in 
GoogleCredentialsHelper.createOAuthCredential() (line 224). The method 
incorrectly calls .setServiceAccountScopes(scopes) on a 
GoogleCredential.Builder that has no service account private key. 
GoogleCredential's constructor requires serviceAccountScopes == null when 
serviceAccountPrivateKey == null, so any non-null scopes collection throws 
IllegalArgumentException. This always breaks google-calendar-stream with OAuth 
(scopes default to CalendarScopes.CALENDAR) and breaks all other Google 
components when users explicitly set scopes with OAuth. Fix: remove the 
.setServiceAccountScopes(scopes) line from createOAuthCredential(). Single-line 
deletion, single PR scope.

_Note: This comment was generated by an AI coding agent and requires manual 
verification._

> 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
>            Assignee: Guillaume Nodet
>            Priority: Major
>
> 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)

Reply via email to