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

Avatar

Avatar
Validate 1
Level 2
mark_julianp181
Level 2

Likes

7 likes

Total Posts

16 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
mark_julianp181
Level 2

Likes

7 likes

Total Posts

16 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Affirm 1
View profile
mark_julianp181
Level 2

07-11-2017

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.

Replies

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

07-11-2017

Have you looked at Maven repo for this JAR?

Avatar

Avatar
Validate 1
Level 2
mark_julianp181
Level 2

Likes

7 likes

Total Posts

16 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
mark_julianp181
Level 2

Likes

7 likes

Total Posts

16 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Affirm 1
View profile
mark_julianp181
Level 2

07-11-2017

Hi Scott,

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

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

07-11-2017

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

Avatar

Avatar
Validate 1
Level 2
mark_julianp181
Level 2

Likes

7 likes

Total Posts

16 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
mark_julianp181
Level 2

Likes

7 likes

Total Posts

16 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Affirm 1
View profile
mark_julianp181
Level 2

07-11-2017

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.