How to use Google Drive API as files source for comparing documents
How to use Google Drive API as files source for comparing documents
Leave feedback
On this page
These days Cloud services are very popular. It means that lots of people use them to store documents, photos and other files. In this situation you may need opportunity to compare files directly from Google Drive, without a need to download and save them locally. You can easily do it combining GroupDocs.Comparison API together with Google Drive API.
Configuring Google API
In order to use Google API you must have Google account. Here we will not describe how to register it. You can find out required steps on Google documentation page. Nevertheless, below are steps, needed to use Google Drive API in your project.
Creating Google Cloud Platform project and activating Drive API
Click Enable APIs and services to enable Google Drive API
Find Google Drive API, select it and click Enable on the opened page
Creating Consent Screen
Open Credentials page and check that selected project is correct
Click Create Credentials and select OAuth client ID
Click Configure Consent Screen button
Select User Type and click Create
Fill App information and click Save and continue
Click Add or remove scopes to configure credential scopes.
On the Update selected scopes screen check needed scopes. You can use filters to search scopes. After selecting all needed scopes, click Update button
Click Save and continue to go to the next step
Add users using Add users button and click Save and continue
Click Back to dashboard to finish configuration
Creating Google Drive API credentials
Open Credentials page and check that selected project is correct
Click Create Credentials and select OAuth client ID
Select application type as Desktop app, enter its name and click Create
Now, copy and save your Client ID, Client secret and of course download and save JSON file, that will be used to access Google Drive API
Configuring project to use Google Drive API with Comparison API
To use Google Drive API as a file storage of Comparison API you need to add some libraries and repository to the project.
Connect GroupDocs repository
MavenGradleKotlin
Libraries
MavenGradleKotlin
Creating Google Drive API objects
First, we need JSON factory, that will be used for communication with Google server
Secondly, we need to create transport object
After that we must create client secrets object. It is the place, where we will use json file that was downloaded after creating API credentials
Next step is to create authorization flow that will be used for authorization request. A little details about options:
DriveScopes.DRIVE - is a scope that was enabled while creating API credentials
"/Path/To/Directory/To/Save/Tokens" - The tokens directory is needed to save authorization tokens. You can remove this line, but in this way Google API will ask for authorization each time.
Now we will request authorization. It will open Google authorization page in default web browser, where you can grand access to the application. More information can be found on Google Documentation page.
Finally, we build an object that will be used to access Google Drive files
Get list of files using Google Drive API
To print list of files which are on your Google Drive, execute next code
In the output each line is one file. It’s ID, name and size. You can use this information to show files to a user. There are lots on data, that can be got for each file. More information can be found on Google Documentation page. For code samples below we will use two of IDs above. Keep in mind, your files will have other IDs, so running code below don’t forget to change IDs.
Comparing Documents directly from Google Drive
GroupDocs.Comparison for Java is a modern, powerful and flexible library that can be used in any Java, Kotlin or other Java compatible projects. You can use it to compare Word, Excel, Presentation documents, photos, images, diagrams, code files, and other supported file formats. Comparison library has lots of settings, as generating summary page, accepting/rejecting changes, comparing more than two documents (multi comparing), generating document preview images, changing comparison sensitivity and changes highlighting styles. More information about settings can be found in the documentation. On our GitHub page you can find samples of integrating Comparison library with different frameworks, as Spring, Dropwizard, Kotlin Compose Desktop and so on.
Saving Result Document Locally
You can easily compare dwo documents, which are located on your Google Drive saving result document locally or saving it into Java stream and use it whatever you want. Keep in mind that 1ZSDTUZArzuwYab0GUbXEoSX_b9kt2mk0 and 1mKk0yt_bBoN9K9mm-3iGXLxfTzkrdw2O are IDs got in the previous steps. For you, they will be different.
In output console you will see path to the result document.
Saving Result Document to Google Drive
Below you can see the code that will compare documents directly from Google Drive. Output files will be uploaded to Google Drive without saving locally anything. After that, you can open Google Drive page in web browser and see result document, for example, using Google Docs.
In output console you will see ID, name and size of uploaded document.
Get a Free API License
Lots of settings, as summary page, sensitivity of comparison and so on are available only with license. You can get a free temporary license in order to use the API without the evaluation limitations.
Conclusion
Reading this article you learned the way to use Google Drive API as a file storage for GroupDocs.Comparison library that gives you lots of opportunities. You can find samples of using Google Drive API inside Spring and Dropwizard applications on GitHub page which you can modify and use as a core of your own application for comparing photos, code files, PowerPoint, Excel, Word and other supported file formats.
For more details, options, and examples, you can go through the documentation and GitHub repository. Reach us on the forum for your queries.