Warm tip: This article is reproduced from serverfault.com, please click

Using Google Drive APis with a scala play project

发布于 2020-12-12 22:12:04

I Have been trying to adapt the java quick start guide for interacting with the google drive API's for a Scala Play project however I am having trouble with reading a the credential file in, it says it is not there however I have tried:

  • Absolute path
  • Path from content root
  • Path from source root

But none of these have worked.

here is my project structure: Proj Structure

Here is the Error: Error

And Here is the Code I am using to try and read the file:

import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver
import com.google.api.client.googleapis.auth.oauth2.{GoogleAuthorizationCodeFlow, GoogleClientSecrets}
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport
import com.google.api.client.http.javanet.NetHttpTransport
import com.google.api.client.json.JsonFactory
import com.google.api.client.json.jackson2.JacksonFactory
import com.google.api.client.util.store.FileDataStoreFactory
import com.google.api.services.sheets.v4.SheetsScopes

import java.io.{File, FileNotFoundException, IOException, InputStreamReader}
import java.security.GeneralSecurityException
import java.util
import java.util.Collections

class GoogleService {
 private val applicationName: String = "Google Sheets API Java Quickstart"
 private val jsonFactory: JsonFactory = JacksonFactory.getDefaultInstance()
 private val tokenDirectoryPath: String = "tokens"

 private val scopes: util.List[String] = Collections.singletonList(SheetsScopes.SPREADSHEETS_READONLY)
 private val credentialsFilePath: String = "app/assets/credentials.json"

 @throws[IOException]
 private def getCredentials(HTTP_TRANSPORT: NetHttpTransport) = { // Load client secrets.
   val in = classOf[GoogleService].getResourceAsStream(credentialsFilePath)
   if (in == null) {
     System.out.printf(credentialsFilePath)
     throw new FileNotFoundException(s"Resource not found: $credentialsFilePath")
   }
   val clientSecrets = GoogleClientSecrets.load(jsonFactory, new InputStreamReader(in))
   // Build flow and trigger user authorization request.
   val flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, jsonFactory, clientSecrets, scopes).setDataStoreFactory(new FileDataStoreFactory(new File(tokenDirectoryPath))).setAccessType("offline").build
   val receiver = new LocalServerReceiver.Builder().setPort(8888).build
   new AuthorizationCodeInstalledApp(flow, receiver).authorize("user")
 }

 @throws[IOException]
 @throws[GeneralSecurityException]
 def main(args: String*
         ) {
   val httpTransport = GoogleNetHttpTransport.newTrustedTransport
   getCredentials(httpTransport)
 }
}
Questioner
DevyDev
Viewed
0
Tomer Shetah 2020-12-14 01:59:46

Try to replace:

val in = classOf[GoogleService].getResourceAsStream(credentialsFilePath)

with:

val in = getClass.getResourceAsStream(credentialsFilePath)

Please note that resources are usually located at the conf folder, and this is where they are looked for. Please try to relocate the folder assets from app folder into conf. Then upadte:

private val credentialsFilePath: String = "assets/credentials.json"