Created
August 3, 2021 19:28
-
-
Save duanebester/6169e48a1171f4b4569b84c4d9e40222 to your computer and use it in GitHub Desktop.
Download files from Google Drive with Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
val scala3Version = "3.0.1" | |
libraryDependencies += "com.google.api-client" % "google-api-client" % "1.32.1", | |
libraryDependencies += "com.google.oauth-client" % "google-oauth-client-jetty" % "1.31.5", | |
libraryDependencies += "com.google.apis" % "google-api-services-drive" % "v3-rev197-1.25.0", | |
*/ | |
import com.google.api.services.drive._ | |
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport | |
import com.google.api.client.json.jackson2.JacksonFactory | |
import com.google.api.client.googleapis.auth.oauth2._ | |
import com.google.api.client.util.store.FileDataStoreFactory | |
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver | |
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp | |
import java.io._ | |
import scala.jdk.CollectionConverters._ | |
import com.google.api.client.http.HttpResponse | |
final val CREDENTIALS_FILE_PATH = "/Users/<USERNAME>/Downloads/client_secret.json" | |
final val OUTPUT_PATH = "/Users/<USERNAME>/Downloads/images" | |
final val APPLICATION_NAME = "App Name - Drive Access"; | |
final val TOKENS_DIRECTORY_PATH = "tokens" | |
final val SCOPES = List( | |
DriveScopes.DRIVE, | |
DriveScopes.DRIVE_FILE, | |
DriveScopes.DRIVE_PHOTOS_READONLY | |
).asJava | |
lazy val HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport() | |
lazy val JSON_FACTORY = JacksonFactory.getDefaultInstance() | |
def getCredentials() = | |
val inStream = new FileInputStream(new File(CREDENTIALS_FILE_PATH)) | |
if (inStream == null) { | |
throw new FileNotFoundException( | |
"Resource not found: " + CREDENTIALS_FILE_PATH | |
) | |
} | |
val clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(inStream)) | |
val flow = new GoogleAuthorizationCodeFlow.Builder( | |
HTTP_TRANSPORT, | |
JSON_FACTORY, | |
clientSecrets, | |
SCOPES | |
).setDataStoreFactory( | |
new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)) | |
).setAccessType("offline") | |
.build() | |
val receiver = new LocalServerReceiver.Builder().setPort(8888).build() | |
new AuthorizationCodeInstalledApp(flow, receiver).authorize("user") | |
@main def main: Unit = | |
val service = | |
new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials()) | |
.setApplicationName(APPLICATION_NAME) | |
.build() | |
val fileId = "<FILE_ID>" | |
val fileGet = service.files().get(fileId).setSupportsAllDrives(true) | |
val meta = fileGet.execute | |
println(meta.getName) | |
val outputStream = new FileOutputStream(new File(s"$OUTPUT_PATH/${meta.getName}")) | |
fileGet.executeMediaAndDownloadTo(outputStream) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment