Error with execution.setVariable()

Hi guys,

I’m trying to configure a process to obtain an access token with a key file from GCP.
The keyfile basically contains a private key and some client information.

It’s related to this topic Flowable and Google Cloud Function

I added a script task to get the token with this groovy script:

@Grab(group='com.google.api.client', module='google-api-client-http', version='1.2.3-alpha')
@Grab(group='com.google.auth', module='google-auth-library-oauth2-http', version='0.20.0')

import com.google.auth.oauth2.AccessToken
import com.google.auth.oauth2.GoogleCredentials
import com.google.auth.oauth2.IdTokenProvider
import com.google.auth.oauth2.ServiceAccountCredentials
import groovy.json.*


class DWHAuthenticator {
    private final String CLIENT_ID = "221881691316-24afsu9eklcjlvjl7rhr9o6t26hokuhl.apps.googleusercontent.com"
    private final String IAM_SCOPE = "https://www.googleapis.com/auth/iam"
    private String credentialsFilePath = "/etc/dwh-credentials/dwh-secupi-dev.json"
    
    public DWHAuthenticator(){
    }
    
    private String authorize() throws Exception {
        File credentialsPath = new File(credentialsFilePath)
        GoogleCredentials credentials = null
        FileInputStream serviceAccountStream = new FileInputStream(credentialsPath.getCanonicalPath())
        credentials = ServiceAccountCredentials.fromStream(serviceAccountStream)
        if (credentials.createScopedRequired()) {
            credentials = (ServiceAccountCredentials) credentials.createScoped(Collections.singleton(IAM_SCOPE))
        }
        credentials.refreshIfExpired()
        AccessToken token = credentials.getAccessToken()
        if (credentials == null || !(credentials instanceof IdTokenProvider)) {
            throw new Exception("Google credentials : credentials that can provide id tokens expected")
        }
        return token
    }
    
    DWHAuthenticator authentication = new DWHAuthenticator()
    def tmp_token = authentication.authorize()
    def access_token = tmp_token.substring(((tmp_token.indexOf('='))+1),tmp_token.indexOf(','))
    execution.setVariable("access_token", access_token)

}

Unfortunately it is giving me this error:

1 error
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:162) ~[groovy-jsr223-2.5.2.jar!/:2.5.2]
        at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233) ~[?:1.8.0_181]
        at org.flowable.common.engine.impl.scripting.ScriptingEngines.evaluate(ScriptingEngines.java:87) ~[flowable-engine-common-6.4.0.jar!/:6.4.0]
        ... 24 more
Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script30.groovy: 37: unexpected token: execution @ line 37, column 5.
       execution.setVariable("access_token", access_token)

I used the execution.setVariable() before and it was never a problem. What could it be?

Thanks :slight_smile:

Seems to me that you’ve unintentionally put the “execution” in the wrong scope. Here, try this:

@Grab(group='com.google.api.client', module='google-api-client-http', version='1.2.3-alpha')
@Grab(group='com.google.auth', module='google-auth-library-oauth2-http', version='0.20.0')

import com.google.auth.oauth2.AccessToken
import com.google.auth.oauth2.GoogleCredentials
import com.google.auth.oauth2.IdTokenProvider
import com.google.auth.oauth2.ServiceAccountCredentials
import groovy.json.*

class DWHAuthenticator {
    private final String CLIENT_ID = "221881691316-24afsu9eklcjlvjl7rhr9o6t26hokuhl.apps.googleusercontent.com"
    private final String IAM_SCOPE = "https://www.googleapis.com/auth/iam"
    private String credentialsFilePath = "/etc/dwh-credentials/dwh-secupi-dev.json"
    
    public DWHAuthenticator(){
    }
    
    public String authorize() throws Exception {
        File credentialsPath = new File(credentialsFilePath)
        GoogleCredentials credentials = null
        FileInputStream serviceAccountStream = new FileInputStream(credentialsPath.getCanonicalPath())
        credentials = ServiceAccountCredentials.fromStream(serviceAccountStream)
        if (credentials.createScopedRequired()) {
            credentials = (ServiceAccountCredentials) credentials.createScoped(Collections.singleton(IAM_SCOPE))
        }
        credentials.refreshIfExpired()
        AccessToken token = credentials.getAccessToken()
        if (credentials == null || !(credentials instanceof IdTokenProvider)) {
            throw new Exception("Google credentials : credentials that can provide id tokens expected")
        }
        return token
    }
}

DWHAuthenticator authentication = new DWHAuthenticator()
def tmp_token = authentication.authorize()
def access_token = tmp_token.substring(((tmp_token.indexOf('='))+1),tmp_token.indexOf(','))
execution.setVariable("access_token", access_token)

Thanks @fiki574 for your fast reply, but unfortunately I still get the same error.