title: Custom google app script doesn't work after copying spreadsheet with google java client
body: <p>I have google spreadsheet template with custom add-on which I'm trying to copy</p>
  
  <pre><code>def copyTemplateSpreadsheet(Drive driveService) {
      File templateCopy = new File()
      templateCopy.setName("excel-template")
  
      def copiedFile = driveService.files().copy(templateSpreadsheetId, templateCopy).execute()
      setCorrectPermission(driveService, copiedFile.getId())
      copiedFile
  }
  
  private void setCorrectPermission(Drive driveService, def fileId) {
      Permission newPermission = new Permission();
      newPermission.setType("anyone");
      newPermission.setRole("writer");
      driveService.permissions().create(fileId, newPermission).execute();
  }
  </code></pre>
  
  <p>The problem is that copied spreadsheet has broken add-on (isn't displayed in add-ons menu). There is correct add-on code in script editor but when I try to run any function I get error message </p>
  
  <pre><code>"We're sorry, a server error occurred. Please wait a bit and try again"
  </code></pre>
  
  <p>Keep in mind that the very same code work well in my template spreadsheet. Even if I delete all the code and leave empty onOpen function the error still appears.</p>
  
  <p>Copying add-ons works well when I do it using regular google drive website (drive.google.com) and also worked when I tried to use google's API Explorer (<a href="https://developers.google.com/drive/v3/reference/files/copy#try-it" rel="nofollow">https://developers.google.com/drive/v3/reference/files/copy#try-it</a>). The problem seems to only when using sdk (at least java one - I haven't tried any other)</p>
  
  <p>Also keep in mind I'm using google service account created as described in this article <a href="https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount" rel="nofollow">https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount</a></p>
  
  <p>and creating Drive instance with following code</p>
  
  <pre><code> Drive getDriveService() throws GeneralSecurityException, IOException, URISyntaxException {
      HttpTransport httpTransport = new NetHttpTransport();
      JacksonFactory jsonFactory = new JacksonFactory();
      GoogleCredential credential = new GoogleCredential.Builder()
              .setTransport(httpTransport)
              .setJsonFactory(jsonFactory)
              .setServiceAccountId(G_SERVICE_EMAIL)
              .setServiceAccountScopes(Arrays.asList(DriveScopes.DRIVE))
              .setServiceAccountPrivateKeyFromP12File(PKC_12_FILE)
              .build();
      Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
              .setHttpRequestInitializer(credential)
              .build();
  
  
      return service;
  }
  </code></pre>
  
  <p>not sure if it matters though, especially since everything else seems to work just fine</p>
  
  <p>Any fix ideas? I'm open to any workarounds as long as they work.
  Also I would be fine with creating new file and just adding add-on code but it seems like I can't do it with API</p>