Package com.google.api.services.drive

Examples of com.google.api.services.drive.Drive


  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 authoized API client   
  Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build();   
  //Insert a file     
  File body = new File();   
  body.setTitle("My document");   
  body.setDescription("A test document");   
  body.setMimeType("text/plain");       
  java.io.File fileContent = new java.io.File("document.txt");   
  FileContent mediaContent = new FileContent("text/plain", fileContent);   
  File file = service.files().insert(body, mediaContent).execute();   

System.out.println("File ID: " + file.getId())}}
View Full Code Here


          .build();

      credential.setAccessToken(tokens.accessToken);
      credential.setRefreshToken(tokens.refreshToken);

      Drive drive = new Drive.Builder(httpTransport, jsonFactory, credential)
          .setApplicationName("GDrive")
          .build();

      return new GDrive(drive);
View Full Code Here

          .build();

      credential.setAccessToken(tokens.accessToken);
      credential.setRefreshToken(tokens.refreshToken);

      Drive drive = new Drive.Builder(httpTransport, jsonFactory, credential)
          .setApplicationName("GDrive")
          .build();

      return new DriveExecValid(drive);
View Full Code Here

   * @return ID of the added file
   * @throws Exception
   */
  public File addFileInGoogleDrive(Node fileNode) throws Exception {

    Drive driveService = getDriveService();

    String mimeType = fileNode.getNode("jcr:content").getProperty("jcr:mimeType").getString();
    InputStream documentIS = fileNode.getNode("jcr:content").getProperty("jcr:data").getStream();

    // Insert a file
    File body = new File();
    body.setTitle(fileNode.getName());
    body.setMimeType(mimeType);

    // TODO files are created on file system, handle it right...
    java.io.File fileContent = new java.io.File(fileNode.getName());
    OutputStream out = new FileOutputStream(fileContent);
    byte buf[] = new byte[1024];
    int len;
    while ((len = documentIS.read(buf)) > 0) {
      out.write(buf, 0, len);
    }
    out.close();
    documentIS.close();

    FileContent mediaContent = new FileContent(mimeType, fileContent);

    Insert insert = driveService.files().insert(body, mediaContent);
    insert.getMediaHttpUploader().setDirectUploadEnabled(true);
    File file = insert.setConvert(true).execute();

    return file;
  }
View Full Code Here

   *            Mimetype of the document
   * @return InputStream of the content of the document
   * @throws IOException
   */
  public File getFile(String fileId) throws IOException, GeneralSecurityException {
    Drive driveService = getDriveService();

    return driveService.files().get(fileId).execute();
  }
View Full Code Here

   * @param mimeType Mimetype of the file
   * @return InputStream of the content of the file
   * @throws IOException
   */
  public InputStream getFileContent(String fileId, String mimeType) throws IOException, GeneralSecurityException {
    Drive driveService = getDriveService();

    File file = getFile(fileId);
   
    String url = (String) file.getExportLinks().get(mimeType);
    if (url != null && url.length() > 0) {
      HttpResponse resp = driveService.getRequestFactory().buildGetRequest(new GenericUrl(url)).execute();
      return resp.getContent();
    } else {
      throw new IOException("No export URL available for the file " + fileId + " with the mimetype " + mimeType);
    }
  }
View Full Code Here

   * @param fileId
   * @param userEmail
   * @throws Exception
   */
  public void shareFileWith(String fileId, String userEmail) throws Exception {
    Drive driveService = getDriveService();
   
    Permission newPermission = new Permission();

    newPermission.setValue(userEmail);
      newPermission.setType("user");
      newPermission.setRole("writer");
   
    driveService.permissions().insert(fileId, newPermission).setSendNotificationEmails(false).execute();
  }
View Full Code Here

   *
   * @param fileId The ID of the file to remove
   * @throws IOException
   */
  public void removeFileInGoogleDrive(String fileId) throws IOException, GeneralSecurityException {
    Drive driveService = getDriveService();

    driveService.files().delete(fileId).execute();
  }
View Full Code Here

TOP

Related Classes of com.google.api.services.drive.Drive

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.