Drive Builder错误:API Google Drive Spring Boot

时间:2017-12-29 08:18:59

标签: java spring-boot google-api google-drive-api

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

我在流程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,   状态= 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 /谷歌/ API /客户端/ HTTP / HttpTransport; LCOM /谷歌/ API /客户端/ HTTP / HttpRequestInitializer; Ljava /郎/字符串; Ljava /郎/字符串; LCOM /谷歌/ API /客户端/ JSON / JsonObjectParser; LCOM /谷歌/ API /客户端/ googleapis /服务/ GoogleClientRequestInitializer; Ljava /郎/字符串; Z)V]   有根本原因

感谢

1 个答案:

答案 0 :(得分:0)

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