Created
March 19, 2018 07:18
-
-
Save potix2/ea262f8b1ceebda7b20e1372d5e3b96b to your computer and use it in GitHub Desktop.
Call sheets API v4 with a service account
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
name := "sheettest" | |
version := "1.0.0" | |
scalaVersion := "2.11.8" | |
libraryDependencies ++= Seq( | |
("com.google.api-client" % "google-api-client" % "1.22.0"). | |
exclude("com.google.guava", "guava-jdk5"), | |
"com.google.oauth-client" % "google-oauth-client-jetty" % "1.22.0", | |
"com.google.apis" % "google-api-services-sheets" % "v4-rev18-1.22.0", | |
"org.scala-lang" % "scala-library" % scalaVersion.value % "compile" | |
) |
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
import java.io.{File, InputStream} | |
import java.net.URL | |
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential | |
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport | |
import com.google.api.client.http.javanet.NetHttpTransport | |
import com.google.api.client.json.jackson2.JacksonFactory | |
import com.google.api.services.sheets.v4.model._ | |
import com.google.api.services.sheets.v4.{Sheets, SheetsScopes} | |
import scala.collection.JavaConversions._ | |
import scala.collection.JavaConverters._ | |
object App { | |
private val SPREADSHEET_URL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full") | |
private val scopes = List(SheetsScopes.SPREADSHEETS) | |
private val HTTP_TRANSPORT: NetHttpTransport = GoogleNetHttpTransport.newTrustedTransport() | |
private val JSON_FACTORY: JacksonFactory = JacksonFactory.getDefaultInstance() | |
def authorize(stream: InputStream): GoogleCredential = { | |
val credential = GoogleCredential.fromStream(stream).createScoped(scopes) | |
credential.refreshToken() | |
credential | |
} | |
def main(args: Array[String]) = { | |
val credentialStream = getClass.getResourceAsStream("credential.json") | |
val spreadSheetId = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms" | |
val credential = authorize(credentialStream) | |
val service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) | |
.setApplicationName("SHEET_TEST") | |
.build() | |
val sheet = service.spreadsheets().get(spreadSheetId).execute() | |
println(s"Title: ${sheet.getProperties.getTitle}") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment