Google Analytics 4 - io.grpc returns "INTERNAL: Failed to frame message" for making GA4 API request

329 Views Asked by At

I'm using the below to attempt to retrieve Google Analytics numbers for my GA4 property in Netbeans with Maven (Java) viz the official Google example at

https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart-client-libraries

My relevant code:

GoogleCredentials gc = ServiceAccountCredentials.fromStream(this.getClass().getResourceAsStream("/client_secrets.json"))
                    .createScoped(Collections.singletonList(StorageScopes.DEVSTORAGE_FULL_CONTROL));

            BetaAnalyticsDataSettings betaAnalyticsDataSettings = BetaAnalyticsDataSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create(gc)).build();

            BetaAnalyticsDataClient betaAnalyticsDataClient = BetaAnalyticsDataClient.create(betaAnalyticsDataSettings);

            RunReportRequest request
                    = RunReportRequest.newBuilder()
                            .setProperty("properties/" + propertyId)
                            .addDimensions(Dimension.newBuilder().setName("city"))
                            .addMetrics(Metric.newBuilder().setName("activeUsers"))
                            .addDateRanges(DateRange.newBuilder().setStartDate("2023-07-31").setEndDate("today"))
                            .build();
            
            RunReportResponse response = betaAnalyticsDataClient.runReport(request);

An execption is raised on the line

RunReportResponse response = betaAnalyticsDataClient.runReport(request);

to whit the core problem is

Caused by: java.lang.UnsupportedOperationException: This is supposed to be overridden by subclasses.

as below:

io.grpc.StatusRuntimeException: INTERNAL: Failed to frame message
    at io.grpc.Status.asRuntimeException(Status.java:530) ~[grpc-api-1.55.3.jar:1.55.3]
    at io.grpc.internal.MessageFramer.writePayload(MessageFramer.java:154) ~[grpc-core-1.55.3.jar:1.55.3]
    at io.grpc.internal.AbstractStream.writeMessage(AbstractStream.java:66) ~[grpc-core-1.55.3.jar:1.55.3]
    at io.grpc.internal.DelayedStream$6.run(DelayedStream.java:283) ~[grpc-core-1.55.3.jar:1.55.3]
    at io.grpc.internal.DelayedStream.drainPendingCalls(DelayedStream.java:182) ~[grpc-core-1.55.3.jar:1.55.3]
    at io.grpc.internal.DelayedStream.access$100(DelayedStream.java:44) ~[grpc-core-1.55.3.jar:1.55.3]
    at io.grpc.internal.DelayedStream$4.run(DelayedStream.java:148) ~[grpc-core-1.55.3.jar:1.55.3]
    at io.grpc.internal.MetadataApplierImpl.finalizeWith(MetadataApplierImpl.java:111) ~[grpc-core-1.55.3.jar:1.55.3]
    at io.grpc.internal.MetadataApplierImpl.apply(MetadataApplierImpl.java:78) ~[grpc-core-1.55.3.jar:1.55.3]
    at io.grpc.auth.GoogleAuthLibraryCallCredentials$1.onSuccess(GoogleAuthLibraryCallCredentials.java:137) ~[grpc-auth-1.55.3.jar:1.55.3]
    at com.google.auth.oauth2.OAuth2Credentials$FutureCallbackToMetadataCallbackAdapter.onSuccess(OAuth2Credentials.java:550) ~[google-auth-library-oauth2-http-1.19.0.jar:na]
    at com.google.auth.oauth2.OAuth2Credentials$FutureCallbackToMetadataCallbackAdapter.onSuccess(OAuth2Credentials.java:541) ~[google-auth-library-oauth2-http-1.19.0.jar:na]
    at com.google.common.util.concurrent.Futures$CallbackListener.run(Futures.java:1138) ~[guava-31.1-jre.jar:na]
    at com.google.common.util.concurrent.DirectExecutor.execute(DirectExecutor.java:31) ~[guava-31.1-jre.jar:na]
    at com.google.common.util.concurrent.AbstractFuture.executeListener(AbstractFuture.java:1270) ~[guava-31.1-jre.jar:na]
    at com.google.common.util.concurrent.AbstractFuture.complete(AbstractFuture.java:1038) ~[guava-31.1-jre.jar:na]
    at com.google.common.util.concurrent.AbstractFuture.set(AbstractFuture.java:783) ~[guava-31.1-jre.jar:na]
    at com.google.auth.oauth2.OAuth2Credentials$RefreshTask.access$400(OAuth2Credentials.java:600) ~[google-auth-library-oauth2-http-1.19.0.jar:na]
    at com.google.auth.oauth2.OAuth2Credentials$RefreshTask$1.onSuccess(OAuth2Credentials.java:617) ~[google-auth-library-oauth2-http-1.19.0.jar:na]
    at com.google.auth.oauth2.OAuth2Credentials$RefreshTask$1.onSuccess(OAuth2Credentials.java:614) ~[google-auth-library-oauth2-http-1.19.0.jar:na]
    at com.google.common.util.concurrent.Futures$CallbackListener.run(Futures.java:1138) ~[guava-31.1-jre.jar:na]
    at com.google.common.util.concurrent.DirectExecutor.execute(DirectExecutor.java:31) ~[guava-31.1-jre.jar:na]
    at com.google.common.util.concurrent.ExecutionList.executeListener(ExecutionList.java:143) ~[guava-31.1-jre.jar:na]
    at com.google.common.util.concurrent.ExecutionList.execute(ExecutionList.java:132) ~[guava-31.1-jre.jar:na]
    at com.google.common.util.concurrent.ListenableFutureTask.done(ListenableFutureTask.java:111) ~[guava-31.1-jre.jar:na]
    at java.base/java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:381) ~[na:na]
    at java.base/java.util.concurrent.FutureTask.set(FutureTask.java:232) ~[na:na]
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:272) ~[na:na]
    at com.google.auth.oauth2.OAuth2Credentials$RefreshTask.run(OAuth2Credentials.java:633) ~[google-auth-library-oauth2-http-1.19.0.jar:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[na:na]
    at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Caused by: java.lang.UnsupportedOperationException: This is supposed to be overridden by subclasses.
    at com.google.protobuf.GeneratedMessageV3.getUnknownFields(GeneratedMessageV3.java:302) ~[protobuf-java-3.21.2.jar:na]
    at com.google.analytics.data.v1beta.Dimension.getSerializedSize(Dimension.java:230) ~[proto-google-analytics-data-v1beta-0.32.0.jar:0.32.0]
    at com.google.protobuf.CodedOutputStream.computeMessageSizeNoTag(CodedOutputStream.java:877) ~[protobuf-java-3.21.2.jar:na]
    at com.google.protobuf.CodedOutputStream.computeMessageSize(CodedOutputStream.java:661) ~[protobuf-java-3.21.2.jar:na]
    at com.google.analytics.data.v1beta.RunReportRequest.getSerializedSize(RunReportRequest.java:933) ~[proto-google-analytics-data-v1beta-0.32.0.jar:0.32.0]
    at io.grpc.protobuf.lite.ProtoInputStream.available(ProtoInputStream.java:108) ~[grpc-protobuf-lite-1.55.3.jar:1.55.3]
    at io.grpc.internal.MessageFramer.getKnownLength(MessageFramer.java:208) ~[grpc-core-1.55.3.jar:1.55.3]
    at io.grpc.internal.MessageFramer.writePayload(MessageFramer.java:138) ~[grpc-core-1.55.3.jar:1.55.3]
    ... 30 common frames omitted

Why is this? Do I have some kind of mismatch in my pom.xml?

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.verishare</groupId>
    <artifactId>WPAnalyticsHelper</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>
    
    <!--Spring-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>
    
        <!--Log4J-->    
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.10.0</version>
        </dependency>
        
        <!--Google APIs Java client-->
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client</artifactId>
            <version>1.35.2</version>
            <type>jar</type>
        </dependency>        
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client-java6</artifactId>
            <version>1.35.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.oauth-client</groupId>
            <artifactId>google-oauth-client-java6</artifactId>
            <version>1.34.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.oauth-client/google-oauth-client-jetty -->
        <dependency>
            <groupId>com.google.oauth-client</groupId>
            <artifactId>google-oauth-client-jetty</artifactId>
            <version>1.34.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client-servlet</artifactId>
            <version>1.35.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client-appengine</artifactId>
            <version>1.35.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client-gson</artifactId>
            <version>1.35.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client-jackson2</artifactId>
            <version>1.35.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client-protobuf</artifactId>
            <version>1.35.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client-xml</artifactId>
            <version>1.35.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-analyticsreporting</artifactId>
            <version>v4-rev20190318-1.28.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-analytics</artifactId>
            <version>v3-rev134-1.22.0</version>
        </dependency>        
        <dependency>
            <groupId>com.google.analytics</groupId>
            <artifactId>google-analytics-data</artifactId>
            <version>0.32.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.auth</groupId>
            <artifactId>google-auth-library-oauth2-http</artifactId>
            <version>1.19.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-storage</artifactId>
            <version>v1-rev58-1.21.0</version>
        </dependency>
    </dependencies>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <java.version>1.8</java.version>
    </properties>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.verishare.wpanalyticshelper.Application</mainClass>
                    <layout>ZIP</layout>
                </configuration>
            </plugin>
            
            <plugin>
                <artifactId>maven-javadoc-plugin</artifactId>
                <configuration>
                    <links>
                        <link>http://download.oracle.com/javase/7/docs/api/</link>
                        <link>https://googleapis.dev/java/google-http-client/${project.http.version}/</link>
                    </links>
                    <doctitle>${project.name} ${project.version}</doctitle>
                    <windowtitle>${project.artifactId} ${project.version}</windowtitle>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        </manifest>
                        <manifestEntries>
                            <Automatic-Module-Name>google.api.client.java6</Automatic-Module-Name>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.5.4</version>
                <executions>
                    <execution>
                        <id>bundle-manifest</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>manifest</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <instructions>
                        <Bundle-DocURL>https://googleapis.dev/java/google-api-client/${project.version}/index.html</Bundle-DocURL>
                        <Bundle-Description>Java 6 extensions to the Google APIs Client Library for Java</Bundle-Description>
                        <Bundle-SymbolicName>com.google.api.client.java6</Bundle-SymbolicName>
                    </instructions>
                </configuration>
            </plugin>
            <!--            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>source-jar</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>-->
            <!--Set minimum requirement to Java 7-->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>animal-sniffer-maven-plugin</artifactId>
                <configuration>
                    <signature>
                        <groupId>org.codehaus.mojo.signature</groupId>
                        <artifactId>java17</artifactId>
                        <version>1.0</version>
                    </signature>
                </configuration>
            </plugin>
                             
        </plugins>
    </build>
</project>

Thanks

1

There are 1 best solutions below

0
Stefan On

With the same Java code as above, updating all the GA4 Maven artifacts in my project's pom.xml to point to the current versions of the Maven artifacts solved the above issue.

Eg. with these imports and the above code:

import com.google.analytics.data.v1beta.BetaAnalyticsDataClient;
import com.google.analytics.data.v1beta.BetaAnalyticsDataSettings;
import com.google.analytics.data.v1beta.DateRange;
import com.google.analytics.data.v1beta.Dimension;
import com.google.analytics.data.v1beta.Metric;
import com.google.analytics.data.v1beta.RunReportRequest;
import com.google.analytics.data.v1beta.RunReportResponse;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.api.services.storage.StorageScopes;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials;

and this pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.verishare</groupId>
    <artifactId>WPAnalyticsHelper</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>
    
    <!--Spring-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>
    
        <!--Log4J-->    
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.10.0</version>
        </dependency>
        
        <!--Google APIs Java client-->
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client</artifactId>
            <version>1.35.2</version>
            <type>jar</type>
        </dependency>        
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client-java6</artifactId>
            <version>1.35.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.oauth-client</groupId>
            <artifactId>google-oauth-client-java6</artifactId>
            <version>1.34.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.oauth-client/google-oauth-client-jetty -->
        <dependency>
            <groupId>com.google.oauth-client</groupId>
            <artifactId>google-oauth-client-jetty</artifactId>
            <version>1.34.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client-servlet</artifactId>
            <version>1.35.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client-appengine</artifactId>
            <version>1.35.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client-gson</artifactId>
            <version>1.35.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client-jackson2</artifactId>
            <version>1.35.2</version>
        </dependency>
        <!--
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client-protobuf</artifactId>
            <version>1.35.2</version>
        </dependency>
    -->
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.23.4</version>
        </dependency>
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client-xml</artifactId>
            <version>1.35.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-analyticsreporting</artifactId>
            <version>v4-rev20190318-1.28.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-analytics</artifactId>
            <version>v3-rev134-1.22.0</version>
        </dependency>        
        <dependency>
            <groupId>com.google.analytics</groupId>
            <artifactId>google-analytics-data</artifactId>
            <version>0.33.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.auth</groupId>
            <artifactId>google-auth-library-oauth2-http</artifactId>
            <version>1.19.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-storage</artifactId>
            <version>v1-rev58-1.21.0</version>
        </dependency>
    </dependencies>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <java.version>1.8</java.version>
    </properties>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.verishare.wpanalyticshelper.Application</mainClass>
                    <layout>ZIP</layout>
                </configuration>
            </plugin>
            
            <plugin>
                <artifactId>maven-javadoc-plugin</artifactId>
                <configuration>
                    <links>
                        <link>http://download.oracle.com/javase/7/docs/api/</link>
                        <link>https://googleapis.dev/java/google-http-client/${project.http.version}/</link>
                    </links>
                    <doctitle>${project.name} ${project.version}</doctitle>
                    <windowtitle>${project.artifactId} ${project.version}</windowtitle>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        </manifest>
                        <manifestEntries>
                            <Automatic-Module-Name>google.api.client.java6</Automatic-Module-Name>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.5.4</version>
                <executions>
                    <execution>
                        <id>bundle-manifest</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>manifest</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <instructions>
                        <Bundle-DocURL>https://googleapis.dev/java/google-api-client/${project.version}/index.html</Bundle-DocURL>
                        <Bundle-Description>Java 6 extensions to the Google APIs Client Library for Java</Bundle-Description>
                        <Bundle-SymbolicName>com.google.api.client.java6</Bundle-SymbolicName>
                    </instructions>
                </configuration>
            </plugin>
            <!--            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>source-jar</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>-->
            <!--Set minimum requirement to Java 7-->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>animal-sniffer-maven-plugin</artifactId>
                <configuration>
                    <signature>
                        <groupId>org.codehaus.mojo.signature</groupId>
                        <artifactId>java17</artifactId>
                        <version>1.0</version>
                    </signature>
                </configuration>
            </plugin>
                             
        </plugins>
    </build>
</project>

my core issue of the thrown UnsupportedOperationException was solved and the exception was no longer raised / thrown.