我目前正在研究一個 Scala 代碼,它可以使用 AD 令牌建立到 SQL 服務器資料庫的連接。
關于這個主題的在線檔案太少,所以我嘗試使用 python 來處理它。現在它正在作業,我希望將我的代碼轉換為 Scala。
這是python腳本:
context = adal.AuthenticationContext(AUTHORITY_URL)
token = context.acquire_token_with_client_credentials("https://database.windows.net/", CLIENT_ID, CLIENT_SECRET)
access_token = token["accessToken"]
df= spark.read \
.format("com.microsoft.sqlserver.jdbc.spark") \
.option("url", URL) \
.option("dbtable", "tab1") \
.option("accessToken", access_token) \
.option("hostNameInCertificate", "*.database.windows.net") \
.load()
df.show()
uj5u.com熱心網友回復:
以下是您可以使用acquireToken函式作為基礎的Java 代碼:
import com.microsoft.aad.adal4j.AuthenticationContext;
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.aad.adal4j.ClientCredential;
...
String authority = "https://login.microsoftonline.com/<org-uuid>;
ExecutorService service = Executors.newFixedThreadPool(1);
AuthenticationContext context = new AuthenticationContext(authority, true, service);
ClientCredential credential = new ClientCredential("sp-client-id", "sp-client-secret");
AuthenticationResult result = context.acquireToken("resource_id", credential, null).get();
// get token
String token = result.getAccessToken()
PS 但實際上,不再推薦 ADAL 的使用,最好改用MSAL(這里是遷移指南)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/336038.html
