Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

AEM with google drive API integration

mark_julianp181
Level 2
Level 2

Hi Everyone,

Does anyone have any experience using the google drive API in AEM? I already made and tested the Java code to make the necessary calls to the API but I'm having trouble integrating it with AEM. I already have the google jars as an OSGi bundle and I have the code below to populate and return a list of custom DriveFile Objects.

@Component

@Service(value=DriveManager.class)

public class DriveManager {

static Logger logger = LoggerFactory.getLogger("DriveManager");

    private static final String APPLICATION_NAME =

        "AEM Drive Manager";

    private static final java.io.File DATA_STORE_DIR = new java.io.File(

        System.getProperty("user.home"), ".credentials/drive-java-read");

    private static FileDataStoreFactory DATA_STORE_FACTORY;

    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

    private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

    private static final List<String> SCOPES =

        Arrays.asList(DriveScopes.DRIVE_METADATA_READONLY);

   

    private static GoogleAuthorizationCodeFlow flow;

   

    static {

        try {

            DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);

        } catch (Throwable i) {

             logger.error("#############IOException : "+i.getMessage());

        }

    }

   

    public static Credential authorize() throws IOException {

  

    InputStream in =

        DriveManager.class.getResourceAsStream("/client_secret.json");

  

    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT,

    JSON_FACTORY, clientSecrets, SCOPES).setDataStoreFactory(DATA_STORE_FACTORY)

            .setAccessType("offline")

            .build();

       

        Credential credential = new AuthorizationCodeInstalledApp(

            flow, new LocalServerReceiver()).authorize("user");

        System.out.println(

                "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());

        return credential;

    }

    public static Drive getDriveService() throws IOException {

        Credential credential = authorize();

        return new Drive.Builder(

                HTTP_TRANSPORT, JSON_FACTORY, credential)

                .setApplicationName(APPLICATION_NAME)

                .build();

    }

    public List<DriveFile> getFileList(){

          logger.info("############# ENTERING GET FILE LIST");

          List<DriveFile> driveFileList = new ArrayList<DriveFile>();

  

          try {

               Drive service = getDriveService();

               FileList results = service.files().list()

              .setPageSize(10)

              .setFields("nextPageToken, files(id, name)")

              .execute();

              List<File> files = results.getFiles();

              if (files == null || files.size() == 0) {

                  return driveFileList;

              } else {

                   for (File file : files) {

                         DriveFile temp = new DriveFile();

                         temp.setId(file.getId());

                         temp.setName(file.getName());

                         temp.setMimeType(file.getMimeType());

                         driveFileList.add(temp);

                   }

              }

          } catch (IOException e) {

               logger.error("#############IO Exception when getting File List "+e.toString());

          }

          for(DriveFile temp : driveFileList){

               logger.info("############# FILE " + temp.getName());

          }

          return driveFileList;

     }

}

Unfortunately I am having the following errors below:

08.11.2017 13:03:17.155 *INFO* [0:0:0:0:0:0:0:1 [1510117396763] GET /content/googledrive/en.html HTTP/1.1] DriveManager ############# ENTERING GET FILE LIST

08.11.2017 13:03:17.159 *ERROR* [0:0:0:0:0:0:0:1 [1510117396763] GET /content/googledrive/en.html HTTP/1.1] com.day.cq.wcm.core.impl.WCMDeveloperModeFilter Error during include of SlingRequestPathInfo: path='/content/googledrive/en/jcr:content/responsivegrid/uploadfile', selectorString='null', extension='html', suffix='null'

java.lang.NoClassDefFoundError: org/mortbay/component/LifeCycle

It says no ClassDefFoundError for org.mortbay.component but I have found no jars for org.mortbay.component and have already added org.mortbay.jetty core and utilities to my OSGi bundle.

Thank you for your help.

4 Replies
smacdonald2008
Level 10
Level 10

Have you looked at Maven repo for this JAR?

mark_julianp181
Level 2
Level 2

Hi Scott,

Yes, I've tried looking for it on maven with no results.

smacdonald2008
Level 10
Level 10

Does this work with Java outside of AEM and adding the JARS to your Java app classpath?

mark_julianp181
Level 2
Level 2

It does work outside AEM with the google API jars, changing the getFileList method to a main method and removing the loggers allows me to run it as a working java app.