I'm trying to run a next code on Google Cloud Functions platform:
Gradle:
apply plugin: 'java'
repositories {
mavenCentral()
}
configurations {
invoker
}
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
dependencies {
compileOnly 'com.google.cloud.functions:functions-framework-api:1.0.4'
invoker 'com.google.cloud.functions.invoker:java-function-invoker:1.2.1'
implementation 'com.google.cloud:google-cloud-storage:2.1.1'
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'com.google.apis:google-api-services-playintegrity:v1-rev20230105-2.0.0'
}
tasks.register("runFunctionGetMetadata", JavaExec) {
main = 'com.google.cloud.functions.invoker.runner.Invoker'
classpath(configurations.invoker)
inputs.files(configurations.runtimeClasspath, sourceSets.main.output)
args('--target', 'com.k.backend.GetMetadata', '--port', 8081)
doFirst {
args('--classpath', files(configurations.runtimeClasspath, sourceSets.main.output).asPath)
}
}
Java:
package com.k.backend;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.playintegrity.v1.PlayIntegrity;
import com.google.api.services.playintegrity.v1.PlayIntegrityRequestInitializer;
import com.google.api.services.playintegrity.v1.model.DecodeIntegrityTokenRequest;
import com.google.api.services.playintegrity.v1.model.DecodeIntegrityTokenResponse;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Optional;
public class GetMetadata implements HttpFunction {
@Override
public void service(HttpRequest request, HttpResponse response) throws IOException {
JsonObject object = new JsonObject();
object.addProperty("calc1", calc(arg + 1)); // arg is just random string
object.addProperty("calc2", calc(arg));
String result = new Gson().toJson(object);
response.setContentType("application/json");
BufferedWriter writer = response.getWriter();
writer.write(result);
}
private String calc(String integrityToken) {
try {
DecodeIntegrityTokenRequest requestObj = new DecodeIntegrityTokenRequest();
requestObj.setIntegrityToken(integrityToken);
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("../client_secret.json"));
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);
HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
JsonFactory JSON_FACTORY = new JacksonFactory();
GoogleClientRequestInitializer initialiser = new PlayIntegrityRequestInitializer();
PlayIntegrity.Builder playIntegrity = new PlayIntegrity.Builder(HTTP_TRANSPORT, JSON_FACTORY, requestInitializer).setApplicationName("AppName")
.setGoogleClientRequestInitializer(initialiser);
PlayIntegrity play = playIntegrity.build();
DecodeIntegrityTokenResponse response = play.v1().decodeIntegrityToken("com.k", requestObj).execute();
return someActionOnResponse(response);
} catch (Exception exception) {
}
return null;
}
}
Once I'm trying to deploy my code to Goole Cloud Functions backend I get error:
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed with status: FAILURE and message: ...k.backend.GetMetadata
[INFO] 11 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.054 s
[INFO] Finished at: 2023-05-16T05:03:52Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project backend: Compilation failure: Compilation failure:
[ERROR] /workspace/src/main/java/com/k/backend/GetMetadata.java:[9,48] package com.google.api.services.playintegrity.v1 does not exist
[ERROR] /workspace/src/main/java/com/k/backend/GetMetadata.java:[10,48] package com.google.api.services.playintegrity.v1 does not exist
[ERROR] /workspace/src/main/java/com/k/backend/GetMetadata.java:[11,54] package com.google.api.services.playintegrity.v1.model does not exist
[ERROR] /workspace/src/main/java/com/k/backend/GetMetadata.java:[12,54] package com.google.api.services.playintegrity.v1.model does not exist
[ERROR] /workspace/src/main/java/com/k/backend/GetMetadata.java:[78,9] cannot find symbol
[ERROR] symbol: class DecodeIntegrityTokenRequest
[ERROR] location: class com.k.backend.GetMetadata
....
What is wrong with my code? Thanks in advance :)
Update: sorry, my bad, I forgot add dependencies to POM file:
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-playintegrity</artifactId>
<version>v1-rev20230105-2.0.0</version>
</dependency>
Now it working fine, thanks!