Drive Builder错误:API Google Drive Spring Boot

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Drive Builder错误:API Google Drive Spring Boot相关的知识,希望对你有一定的参考价值。

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

我在进程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原因

谢谢

答案

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

以上是关于Drive Builder错误:API Google Drive Spring Boot的主要内容,如果未能解决你的问题,请参考以下文章

Google Drive API(Driver.Builder)身份验证问题:NullPointerException前置条件检查

Node.js Google Drive api 下载文件错误

Google Drive API 推送通知订阅 400“解析错误”

已安装 Google Drive API,但出现错误“没有此类模块 'GoogleAPIClient'”

XMLHttpRequest Access-Control-Allow-Origin 错误 Google Drive API

尝试使用 Google Drive API 的错误 JWT 签名