使用google drive api上传文件

时间:2015-04-20 05:19:06

标签: java google-drive-api

我想将文件上传到我的google驱动器。我有插入文件到谷歌驱动器的代码,但它从google身份验证获取请求网址。我不想要这个实现。我想直接上传文件谷歌驱动器使用java。< / p>

我的java代码是:

       `HttpTransport httpTransport = new NetHttpTransport();
        JsonFactory jsonFactory = new JacksonFactory();

        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, IRingeeConstants.GOOGLE_DRIVE_CLIENT_ID, IRingeeConstants.GOOGLE_DRIVE_SCRET,
                Arrays.asList(DriveScopes.DRIVE)).setAccessType("online").setApprovalPrompt("auto").build();

        String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();
        System.out.println("Please open the following URL in your browser then type the authorization code:");
        System.out.println("  " + url);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String code = br.readLine();

        GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
        GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response);

        // Create a new authorized API client
        Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build();

        // Insert a file
        File body = new File();
        body.setTitle("Ringee document");
        body.setDescription("A test document");
        body.setMimeType("text/plain");

        java.io.File fileContent = new java.io.File("G:\\document.txt");
        FileContent mediaContent = new FileContent("text/plain", fileContent);

        File file = service.files().insert(body, mediaContent).execute();
        System.out.println("File ID: " + file.getId());`

我希望直接上传不需要任何插入和访问谷歌驱动器文件的权限。

0 个答案:

没有答案