Unexpected error refreshing access token

hello, I am a student who is currently designing an application by integrating the Google API. I'm currently putting together a test API call and token request. However, I experienced problems when requesting a token. The problem I experienced was: java.io.IOException: Unexpected error refreshing access token. The following is an attachment to my code and error

Code:

package com.example.coba;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import java.io.FileInputStream;
import java.util.Collections;

public class MainActivity {
public static void main(String[] args) {
try {
// Load the service account key JSON file
FileInputStream serviceAccount = new FileInputStream("D:\\SEMENTARA\\ta-yoga-418816-bf5d93710c8d.json");

// Authenticate a GoogleCredential with the service account
GoogleCredentials credential = ServiceAccountCredentials.fromStream(serviceAccount)
.createScoped(Collections.singleton("https://script.googleapis.com/v1/scripts/AKfycbwyHyH2Fp_7E6mR9tomsywXGd83-w59NFx5KHPiCv-_PgHXhpwn_8xFiKi2VRbHGmiv:run?"));

// Use the GoogleCredential to create an authorized HttpTransport
credential.refreshIfExpired();
String accessToken = credential.getAccessToken().getTokenValue();

// Now you can use the access token to authenticate your requests
System.out.println("Access Token: " + accessToken);
} catch (Exception e) {
e.printStackTrace();
if (e instanceof java.io.IOException) {
System.out.println("An IOException occurred. This may be due to a problem with the network connection, or the file system.");
} else if (e instanceof IllegalStateException) {
System.out.println("An IllegalStateException occurred. This may be due to an error in the program logic.");
} else {
System.out.println("An unexpected error occurred: " + e.getMessage());
}
}
}
}

error:
java.io.IOException: Unexpected error refreshing access token
at com.google.auth.oauth2.OAuth2Credentials.unwrapDirectFuture(OAuth2Credentials.java:325)
at com.google.auth.oauth2.OAuth2Credentials.refreshIfExpired(OAuth2Credentials.java:204)
at com.example.coba.MainActivity.main(MainActivity.java:19)
Caused by: java.lang.NoClassDefFoundError: io/grpc/Context
at io.opencensus.trace.unsafe.ContextManagerImpl.currentContext(ContextManagerImpl.java:30)
at io.opencensus.trace.unsafe.ContextHandleUtils.currentContext(ContextHandleUtils.java:56)
at io.opencensus.trace.CurrentSpanUtils.getCurrentSpan(CurrentSpanUtils.java:37)
at io.opencensus.trace.Tracer.spanBuilder(Tracer.java:308)

Help me please

0 REPLIES 0