0

我想使用 valence 和 java 进行身份验证并获取特定的课程组织单位 ID。我有从 d2l keytool 获得的应用程序的应用程序 ID 和用户密钥。我还使用 d2l 的 java 客户端库进行身份验证。即 com.d2lvalence.idkeyauth.*;

我在最后一行代码中收到 http 403 错误。
有人可以看到我做错了什么吗?

        URL url = null;
        URLConnection connection = null;
        String host = "ucbdev.desire2learn.com";
        int port = 443;
        String appID = "from d2l";
        String appKey = "from d2l";
        String userId = "";
        String userKey = "";

        AuthenticationSecurityFactory factory = new AuthenticationSecurityFactory();
        // appID and appKey are from d2l
        ID2LAppContext appContext = factory.createSecurityContext(appID, appKey);

        URI resultUri=new URI("?x_a=fromd2l&x_b=fromd2l");
        ID2LUserContext userContext=appContext.createUserContext(resultUri, host, port, true);
        if (userContext == null){
            System.out.println("USERCONTEXT is NULL");
        }
        System.out.println("USERCONTEXT HOST NAME IS :"+userContext.getHostName());

        userId = userContext.getUserId();
        userKey = userContext.getUserKey();
        System.out.println("userID is "+userId);
        System.out.println("userKey is "+userKey);

        URI newUri = userContext.createAuthenticatedUri("/d2l/api/lp/1.0/orgstructure/", "GET");
        String res = newUri.toString();
        System.out.println("authenticated uri usercontext s "+res);
        connection = newUri.toURL().openConnection();

        //cast the connection to a HttpURLConnection so we can examin the status code
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        httpConnection.setRequestMethod("GET");
        StringBuilder sb=new StringBuilder();

        BufferedReader in = null;
        //if the status code is success then the body is read from the input stream
        if(httpConnection.getResponseCode()==200) {
            in = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
        //otherwise the body is read from the output stream
        } else {
            System.out.println("Error: " + httpConnection.getResponseCode() + ""); //error 403 here
        //    in = new BufferedReader(new InputStreamReader(httpConnection.getErrorStream()));
        }
4

1 回答 1

1

您似乎不清楚Valence Learning Framework API 的身份验证是如何工作的。您从 D2L 的 KeyTool 获取的 AppId/AppKey 对是您将用来证明您的 API 调用来自您的应用程序的密钥对(即,您在正常调用的参数中传递 AppId x_a,并使用 AppKey 生成一个签名,然后您x_c在调用的参数中传递)。但是每个正常的 API 调用还需要用户令牌来证明是代表已知用户进行的:

我们所有的 SDK 都以相同的一般方式工作:

  1. 首先,您创建一个使用 AppID/Key 密钥对构建的应用程序上下文对象。

  2. 然后,您创建一个“用于身份验证的 URL”:此 URL 将调用特殊的“获取用户令牌” API 调用(这里的x_a参数是您的 AppId,x_b参数是签名)。

  3. 您指示用户的浏览器到此 URL 进行身份验证,它的x_target查询参数指定 LMS 在成功确定用户身份后应将用户 ID/Key 对发送到的回调 URL。

  4. 一旦您拥有此用户 ID/密钥对,在随后的正常 API 调用中,您将在x_b参数中传递用户 ID(因为您在 中传递应用程序 ID x_a),并且您将使用用户密钥进行签名将传入x_d参数。

请仔细按照文档中的身份验证概念主题进行操作,因为它将向您展示应用程序获取用户 ID/密钥对的过程中涉及的所有步骤,以便您可以使用它来进行 API 调用。

于 2014-02-27T17:33:54.383 回答