【问题标题】:How to convert this code to scala (using adal to generate Azure AD token)如何将此代码转换为 scala(使用 adal 生成 Azure AD 令牌)
【发布时间】:2021-12-10 18:46:44
【问题描述】:

我目前正在编写一个 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()

【问题讨论】:

    标签: scala apache-spark azure-active-directory adal


    【解决方案1】:

    以下是您可以使用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()
    

    附:不过说真的,ADAL的用法已经不推荐了,最好改用MSAL(这里是migration guide

    【讨论】:

    • 请问如何利用结果?我做了 println(result) 并得到了 com.microsoft.aad.adal4j.AuthenticationResult@c65a3855 我该如何实际使用令牌?
    • 让我更新答案。你得到的令牌为result.getAccessToken()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 2017-12-23
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多