Google Drive SDK(Java)服务帐户上传文件并从浏览器访问

时间:2014-08-01 13:01:09

标签: java google-drive-api

正如我在标题中所提到的,我是从我的java应用程序上传gzip文件,如下所示:

private static File insertFile( Drive service, String title, String description, String parentId, String mimeType, String filename ){
    // File's metadata.
    File body = new File();
    body.setTitle( title );
    body.setDescription( description );
    body.setMimeType( mimeType );

    // Set the parent folder.
    if( parentId != null && parentId.length() > 0 ){
        body.setParents( Arrays.asList( new ParentReference().setId( parentId ) ) );
    }

    // File's content.
    java.io.File fileContent = new java.io.File( filename );
    FileContent mediaContent = new FileContent( mimeType, fileContent );
    try{
        File file = service.files().insert( body, mediaContent ).execute();
        return file;
    }
    catch( IOException e ){
        System.out.println( "An error occured: " + e );
        return null;
    }
}



public static Drive getDriveService() throws GeneralSecurityException, IOException, URISyntaxException{
        HttpTransport httpTransport = new NetHttpTransport();
        JacksonFactory jsonFactory = new JacksonFactory();

        GoogleCredential credential = new GoogleCredential.Builder().setTransport( httpTransport ).setJsonFactory( jsonFactory ).setServiceAccountId( SERVICE_ACCOUNT_EMAIL )
                .setServiceAccountScopes( Arrays.asList( DriveScopes.DRIVE ) ).setServiceAccountPrivateKeyFromP12File( new java.io.File( SERVICE_ACCOUNT_PKCS12_FILE_PATH ) ).build();

        Drive service = new Drive.Builder( httpTransport, jsonFactory, null ).setHttpRequestInitializer( credential ).setApplicationName( "Svn Backup" ).build();
        return service;
    }




public void main()
{
 Drive driveService = getDriveService();
                // Insert a file

                System.out.println( new LocalDateTime().toString( "dd.MM.yyyy HH:mm:ss" ) + ": " + "trying to upload google drive.." );
                File file = insertFile( driveService, new LocalDateTime().toString( "dd_MM_yyyy_HH_mm_ss" ), "", null, "application/x-gzip", args[ 1 ] + ".gz" );
                System.out.println( new LocalDateTime().toString( "dd.MM.yyyy HH:mm:ss" ) + ": " + "File: " + file );
                System.out.println( "successfuly uploaded" );
}

它完美无缺,因为我可以使用files().list()命令访问我的文件,但我无法在https://drive.google.com/

中看到Google驱动器应用程序中的文件

这是什么原因?我该怎么办?

0 个答案:

没有答案