Drive Builder错误:API Google Drive Spring Boot

问题描述 投票:0回答:1

我的代码有问题。你能帮帮我们吗?

我在进程service = new com.google.api.services.drive.Drive.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();上有一个错误代码

这是我的代码:

@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();
    }
}

我想显示我的所有驱动器列表,但它是构建驱动器的约束,这是浏览器的错误:

出现意外错误(type = Internal Server Error,status = 500)。 com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient(LCOM /谷歌/ API /客户端/ HTTP / HttpTransport; LCOM /谷歌/ API /客户端/ HTTP / HttpRequestInitializer; Ljava /郎/字符串; Ljava /郎/字符串; LCOM /谷歌/ API /客户端/ JSON / JsonObjectParser; LCOM /谷歌/ API /客户端/ googleapis /服务/ GoogleClientRequestInitializer; Ljava /郎/字符串; Z)V

和控制台的错误:

ERROR 7756 --- [nio-8080-exec-2] o.a.c.c.C。[。[。[/]。[dispatcherServlet]:servlet [dispatcherServlet]的Servlet.service()与path []的上下文发生异常[Handler dispatch failed;嵌套异常是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原因

谢谢

java spring-boot google-api google-drive-sdk
1个回答
0
投票

如果您遇到Internal Server Error,则此documentation中的建议操作是使用truncated exponential backoff再次尝试。在重试非幂等请求之前,还建议here包含一个检查。

© www.soinside.com 2019 - 2024. All rights reserved.