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 呼び出しがアプリからのものであることを証明するために使用するキーペアです(つまりx_a、通常の呼び出しでパラメーターに AppId を渡し、AppKey を使用して生成します)。x_c呼び出し時にパラメーターに渡す署名)。ただし、通常の API 呼び出しには、既知のユーザーの代わりに行われたことを証明するためのユーザー トークンも必要です。

当社のすべての SDK は、同じ一般的な方法で動作します。

  1. 最初に、AppID/Key キーペアを使用して構築されたアプリケーション コンテキストオブジェクトを作成します。

  2. 次に、「認証用の URL」を作成します。この URL は、特別な「ユーザー トークンの取得」 API 呼び出しへの呼び出しになります (ここでのx_aパラメーターは AppId であり、x_bパラメーターは署名です)。

  3. 認証のためにこの URL にアクセスするようにユーザーのブラウザーに指示しx_targetます。そのクエリ パラメーターは、LMS がユーザーの特定に成功した後にユーザー ID とキーのペアを送信するコールバック URL を指定します。

  4. このユーザー ID とキーのペアを取得したら、後続の通常の API 呼び出しで、x_bパラメーターにユーザー ID を渡し (でアプリ ID を渡すためx_a)、ユーザー キーを使用して署名を作成しますパラメータを渡しx_dます。

ドキュメントの認証の概念に関するトピックに注意深く従ってください。アプリがユーザー ID とキーのペアを取得するプロセスに含まれるすべての手順が示されているため、それを使用して API 呼び出しを行うことができます。

于 2014-02-27T17:33:54.383 に答える