0
votes

I have a problem on my code. Can you help me guys?

I have an error code on process service = new com.google.api.services.drive.Drive.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();

Here is my code:

@Controller
@RestController
public class GoogleOauthController {

    private static final String APPLICATION_NAME = "oauth drive";
    private static HttpTransport httpTransport;
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    private static com.google.api.services.drive.Drive service;

    GoogleClientSecrets clientSecrets;
    GoogleAuthorizationCodeFlow flow;
    Credential credential;

    @Value("${drive.client.clientId}")
    private String clientId;

    @Value("${drive.client.clientSecret}")
    private String clientSecret;

    @Value("${drive.client.redirectUri}")
    private String redirectUri;

    @RequestMapping(value = "/login/drive", method = RequestMethod.GET)
    public RedirectView googleConnectionStatus(HttpServletRequest request) throws Exception {
        return new RedirectView(authorize());
    }

    @RequestMapping(value = "/login/callbackoauth", method = RequestMethod.GET, params = "code")
    public List<File> oauth2Callback(@RequestParam(value = "code") String code) {

        // System.out.println("code->" + code + " userId->" + userId + "
        // query->" + query);

        JSONObject json = new JSONObject();
        JSONArray arr = new JSONArray();

         List<File> result = new ArrayList<File>();

        // String message;
        try {
            TokenResponse response = flow.newTokenRequest(code).setRedirectUri(redirectUri).execute();
            credential = flow.createAndStoreCredential(response, "userID");

            service = new com.google.api.services.drive.Drive.Builder(httpTransport, JSON_FACTORY, credential)
                    .setApplicationName(APPLICATION_NAME).build();
            System.out.println(service);
            Files.List request = service.files().list();

            do {
                  try {
                    FileList files = request.execute();

                    result.addAll(files.getItems());
                    request.setPageToken(files.getNextPageToken());
                  } catch (IOException e) {
                    System.out.println("An error occurred: " + e);
                    request.setPageToken(null);
                  }
                } while (request.getPageToken() != null &&
                         request.getPageToken().length() > 0);


        } catch (Exception e) {

            System.out.println("exception cached ");
            e.printStackTrace();
        }

        return result;
    }

    private String authorize() throws Exception {
        AuthorizationCodeRequestUrl authorizationUrl;
        if (flow == null) {
            Details web = new Details();
            web.setClientId(clientId);
            web.setClientSecret(clientSecret);
            clientSecrets = new GoogleClientSecrets().setWeb(web);
            httpTransport = GoogleNetHttpTransport.newTrustedTransport();
            flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets,
                    Collections.singleton(DriveScopes.DRIVE)).build();
        }
        authorizationUrl = flow.newAuthorizationUrl().setRedirectUri(redirectUri);

        System.out.println("drive authorizationUrl ->" + authorizationUrl.build());
        return authorizationUrl.build();
    }
}

I want to show all the list of my drives, but it's a constraint on the build drive, and this is an error from the browser:

There was an unexpected error (type=Internal Server Error, status=500). com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient.(Lcom/google/api/client/http/HttpTransport;Lcom/google/api/client/http/HttpRequestInitializer;Ljava/lang/String;Ljava/lang/String;Lcom/google/api/client/json/JsonObjectParser;Lcom/google/api/client/googleapis/services/GoogleClientRequestInitializer;Ljava/lang/String;Z)V

and the error from console:

ERROR 7756 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient.(Lcom/google/api/client/http/HttpTransport;Lcom/google/api/client/http/HttpRequestInitializer;Ljava/lang/String;Ljava/lang/String;Lcom/google/api/client/json/JsonObjectParser;Lcom/google/api/client/googleapis/services/GoogleClientRequestInitializer;Ljava/lang/String;Z)V] with root cause

thanks

1

1 Answers

0
votes

If you encountered an Internal Server Error, the suggested action in this documentation is to try again using truncated exponential backoff. It is also recommended here to include a check before retrying non-idempotent requests.