使用Java

时间:2017-04-06 10:14:00

标签: java azure azure-data-catalog

在这里,我尝试在Azure数据目录中注册Data Lake Store中的数据资产。 我正在尝试获取Azure数据目录的身份验证令牌,然后我将在标题中设置如下

request.setRequestProperty("Authorization","Bearer "+accesstoken);

我用来获取令牌的代码

//This method sends request and gets the reponse
public static String SetRequestAndGetResponse(HttpsURLConnection request, String payload)
{ 
    String accesstoken=null;
    ExecutorService service = null;

    Future<AuthenticationResult> FutureResult;
    AuthenticationResult result;
    AuthenticationCallback callback = null;

    //Creating the credential object for DataCatalog with Client ID and Client secret picked up from the vault

    ClientCredential credential = new ClientCredential("client_ID", "client_secret");

    try
    {
        service = Executors.newFixedThreadPool(1);
        AuthenticationContext context = new AuthenticationContext("https://login.windows.net/tenant_ID/oauth2/token",true,service);

        /* 
         * getting the authentication result object using the App ID URI from Azure AD as suggested in
         * 
         * https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-code
         */

        FutureResult = context.acquireToken("App ID URI from Azure AD/login/aad", credential,null);
        while(!(FutureResult.isDone()))
        {}
        accesstoken=FutureResult.get().getAccessToken();
        //System.out.println("result "+accesstoken);
    }
    catch(Exception e)
    {System.out.println("ex "+e.getMessage());
    e.printStackTrace();}

但是,我得到例外,如下所示

ex com.microsoft.aad.adal4j.AuthenticationException:
{“error_description”:“AADSTS50001:在名为tenant_id的租户中找不到名为https://abc.onmicrosoft.com/somecode/login/aad的应用程序。
如果租户管理员尚未安装应用程序或租户中的任何用户同意该应用程序,则会发生这种情况。
您可能已将您的身份验证请求发送给错误的租户。

跟踪ID:some_id

相关ID:some_id

时间戳:2017-04-06 09:57:01Z“,”错误“:”invalid_resource“}

at com.microsoft.aad.adal4j.AdalTokenRequest.executeOAuthRequestAndProcessResponse(AdalTokenRequest.java:107)

在com.microsoft.aad.adal4j.AuthenticationContext.acquireTokenCommon(AuthenticationContext.java:816)

at com.microsoft.aad.adal4j.AuthenticationContext.access $ 100(AuthenticationContext.java:64)

at com.microsoft.aad.adal4j.AuthenticationContext $ 1.call(AuthenticationContext.java:172)

at com.microsoft.aad.adal4j.AuthenticationContext $ 1.call(AuthenticationContext.java:161)

在java.util.concurrent.FutureTask.run(FutureTask.java:266)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)

在java.lang.Thread.run(Thread.java:745)

*****************编辑******************* 谢谢!但是,我尝试使用“https://graph.windows.net”作为资源URI来使用身份验证上下文获取令牌,但服务器仍然响应未经授权的请求。 顺便说一句,我使用下面的代码片段来创建一个请求对象。

String fullUri = String.format("https://api.azuredatacatalog.com/catalogs/"+catalogName+"/views/tables?api-version=2016-03-30");
    URL url = null;
    try {
        //sETTING UP url connection to azure data catalog api 
        url = new URL(fullUri);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        System.out.println("Malformed URL Exception");
    }
    HttpsURLConnection request;
    try {
        request = (HttpsURLConnection) url.openConnection();
        }
        catch(Exception e){ e.printStackTrace();}

请帮我解决这个问题。

感谢。

2 个答案:

答案 0 :(得分:0)

AuthenticationContext.acquireToken方法的第一个参数应该是图形资源,而不是Azure AD中的App ID URI。

请更改以下代码行:

FutureResult = context.acquireToken("https://graph.windows.net", credential, null);

有关详细信息,请参阅:https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-devquickstarts-webapp-java

答案 1 :(得分:0)

以下是您应该使用的内容:

string authorityUri = "https://login.windows.net/common/oauth2/authorize";
AuthenticationContext authContext = new AuthenticationContext(authorityUri);

此外,传递给AcquireToken方法的资源字符串应为&#34; https://api.azuredatacatalog.com&#34;。