title: Migration from Google Drive API from v2 to v3 giving inconsistent results
body: <p>I have a very simple program to list files in a Google Drive. I have written 2 versions, using v2 and v3 of the API. However when I run them I get inconsistent results. v2 works fine, v3 doesn't. They are both written in groovy which is a superset of java. To install groovy use <a href="http://sdkman.io/install.html" rel="nofollow">Sdkman</a></p>
  
  <p>v2.groovy</p>
  
  <pre><code>@GrabResolver(name='com.google.apis', root='http://google-api-client-libraries.appspot.com/mavenrepo')
  @Grapes([
      @Grab(group='com.google.api-client',    module='google-api-client       version='1.12.0-beta'),
      @Grab(group='com.google.apis',      module='google-api-services-drive', version='v2-rev30-1.12.0-beta'),
      @Grab(group='com.google.http-client',   module='google-http-client-jackson',    version='1.12.0-beta')
  ])
  
  import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow
  import com.google.api.client.googleapis.auth.oauth2.GoogleCredential
  import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse
  
  import com.google.api.client.http.HttpTransport
  import com.google.api.client.json.JsonFactory
  
  import com.google.api.client.http.javanet.NetHttpTransport
  import com.google.api.client.json.jackson.JacksonFactory
  
  import com.google.api.services.drive.Drive
  import com.google.api.services.drive.DriveScopes
  import com.google.api.services.drive.model.*
  
  import groovy.json.*
  
  import com.google.api.client.http.FileContent
  
  class GD {
  
      def drive
      def credential
      def httpTransport
      def jsonFactory
      def scopes
  
      def GD (config) {
          httpTransport = new NetHttpTransport ()
          jsonFactory = new JacksonFactory ()
          scopes = DriveScopes.DRIVE
  
          credential = new GoogleCredential.Builder().setTransport(httpTransport)
                  .setJsonFactory(jsonFactory)
                  .setServiceAccountId(config.accountId)
                  .setServiceAccountScopes(scopes)
                  .setServiceAccountPrivateKeyFromP12File(new java.io.File(config.p12File))
                  .setServiceAccountUser (config.accountUser)
                  .build();
  
          credential.refreshToken();
  
          drive = new Drive.Builder(httpTransport, jsonFactory, credential).setApplicationName(config.applicationName).build()
      }
  
      def getAllFiles() throws IOException {
          def list = []
          def request = drive.files().list()
  
          while (true) {
              def files = request.execute()
              list += files.getItems()
              request.pageToken = files.nextPageToken
  
              if (!request.pageToken)
                  break
  
              if (list.size() % 10 == 0)
                  print "${list.size()} ...\r"
          }
  
          println "${list.size()}"
          return list
      }
  }
  
  def config = [
          accountId: '13242342543534653', // replace by your user id
          accountUser: 'xxxx@appspot.gserviceaccount.com', // replace by your account email
          p12File: '/home/pascal/wep-ffah-f01d3bfec317.p12', // replace by path to your p12File
          applicationName: 'myapp',   // replace by your app
  ]
  def drive = new GD (config)
  println "retrieving files"
  drive.getAllFiles().each  { println "${it.id} ${it.title}: ${it.mimeType}" }
  </code></pre>
  
  <p>v3.groovy</p>
  
  <pre><code>@GrabResolver(name='com.google.apis', root='http://google-api-client-libraries.appspot.com/mavenrepo')
  @Grapes([
      @Grab(group='com.google.api-client',    module='google-api-client',     version='1.21.0'),
      @Grab(group='com.google.http-client',   module='google-http-client-jackson2',   version='1.21.0'),
      @Grab(group='com.google.apis',      module='google-api-services-drive', version='v3-rev16-1.21.0')
      // @Grab(group='com.google.apis',       module='google-api-services-prediction',version='v1.6-rev59-1.21.0'),
  
  ])
  
  import com.google.api.client.googleapis.auth.oauth2.GoogleCredential
  
  import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport
  import com.google.api.client.json.jackson2.JacksonFactory
  
  // import com.google.api.services.prediction.PredictionScopes
  import com.google.api.services.drive.Drive
  import com.google.api.services.drive.DriveScopes
  import com.google.api.services.drive.model.*
  
  import groovy.json.*
  
  import java.util.Collections
  
  class GD {
      def drive
      def credential
      def httpTransport
      def jsonFactory
      def scopes
  
      def GD (config) {
          httpTransport = GoogleNetHttpTransport.newTrustedTransport()
          jsonFactory = JacksonFactory.getDefaultInstance()
          scopes = Collections.singleton (DriveScopes.DRIVE)
  
          credential = GoogleCredential.fromStream (new FileInputStream (config.jsonFile), httpTransport, jsonFactory)
          credential.serviceAccountScopes = scopes
          drive = new Drive.Builder(httpTransport, jsonFactory, credential).setApplicationName(config.applicationName).build()
      }
  
      def getAllFiles() throws IOException {
          def list = []
          def request = drive.files().list()
  
          while (true) {
              def files = request.execute()
              list += files.getFiles()
              request.pageToken = files.getNextPageToken()
  
              if (!request.pageToken &amp;&amp; request.pageToken.length &gt; 0)
                  break
  
              if (list.size() % 10 == 0)
                  print "${list.size()} ...\r"
  
          }
  
          println "${list.size()}"
          return list
      }
  }
  
  /************************************** Start here ******************************************/
  
  def config = [
      jsonFile: '/home/myhome/yourapp-b954bxxxxx.json', // repalce by your json credential file
      applicationName: 'yourapp', // set your app
  ]
  
  def drive = new GD (config)
  println "retrieving files"
  drive.getAllFiles ().each  { println "${it.id} ${it.name}: ${it.mimeType}" }
  </code></pre>