Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2025 03:12 AM
If I generate the Auth Token by my self and use this token, it also works.
Create Token:
import java.net.{URL, HttpURLConnection}
import java.io.{OutputStreamWriter}
import scala.io.Source
import scala.util.parsing.json.JSON
// Databricks OAuth client credentials
val clientId = "<CLIENT_ID"
val clientSecret = "<DATABRICKS_GENERATED_SP_SECRET>"
val tokenEndpoint = "https://<HOST>/oidc/v1/token"
val requestBody =
s"grant_type=client_credentials&client_id=$clientId&client_secret=$clientSecret&scope=all-apis"
val url = new URL(tokenEndpoint)
val conn = url.openConnection().asInstanceOf[HttpURLConnection]
conn.setRequestMethod("POST")
conn.setDoOutput(true)
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
val writer = new OutputStreamWriter(conn.getOutputStream)
writer.write(requestBody)
writer.flush()
writer.close()
val response = Source.fromInputStream(conn.getInputStream).mkString
val parsed = JSON.parseFull(response).get.asInstanceOf[Map[String, Any]]
val accessToken = parsed("access_token").asInstanceOf[String]Use Auth Token without error:
import java.sql.{Connection, DriverManager}
Class.forName("com.databricks.client.jdbc.Driver")
val jdbcUrl = s"jdbc:databricks://<HOST>:443;httpPath=<HTTP_PATH>;AuthMech=11;Auth_Flow=0;Auth_AccessToken=$accessToken"
val conn: Connection = DriverManager.getConnection(jdbcUrl)
val stmt = conn.createStatement()
val rs = stmt.executeQuery("SELECT current_user(), current_date()")
while (rs.next()) {
println(s"${rs.getString(1)}\t${rs.getString(2)}")
}
rs.close()
stmt.close()
conn.close()