Search code examples
desire2learnvalence

403 Invalid token error on GET user info


UPDATE: I thought I had to pass the parameters as a JSON string in the request body, but actually I need to put them on the URL (the endpoint string), so it's working now.

I'm new to Valence. I have some Salesforce Apex code (written by someone else) that creates a D2L user. The code is working fine.

I want to add an Apex method to retrieve info for an existing D2L user using the userName parameter. I've copied the existing method, changed to a GET, set the query parameter to userName, and kept everything else the same.

When I call my method, I get a 403 Invalid Token error.

Do I need to use different authorization parameters for a GET? For example, do I still need to include a timestamp?

Here's a portion of the Salesforce Apex code:

public static final String USERS = '/d2l/api/lp/1.0/users/';

    String TIMESTAMP_PARAM_VALUE = String.valueOf(Datetime.now().getTime()).substring(0,10);
    String method = GETMETHOD;
    String action = USERS;
    String signData = method + '&' + action + '&' + TIMESTAMP_PARAM_VALUE;
    String userSignature = sign(signData,USER_KEY);
    String appSignature = sign(signData,APP_KEY);
    String SIGNED_USER_PARAM_VALUE = userSignature;
    String SIGNED_APP_PARAM_VALUE = appSignature;
    String endPoint = DOMAIN + action + '?' + 
            APP_ID_PARAM + '=' + APP_ID + '&' + 
            USER_ID_PARAM + '=' + USER_ID + '&' +
            SIGNED_USER_PARAM + '=' + SIGNED_USER_PARAM_VALUE + '&' + 
            SIGNED_APP_PARAM + '=' + SIGNED_APP_PARAM_VALUE + '&' + 
            TIMESTAMP_PARAM + '=' + TIMESTAMP_PARAM_VALUE; 

    HttpRequest req = new HttpRequest();
    req.setMethod(method);
    req.setTimeout(30000);
    req.setEndpoint(endPoint);
    req.setBody('{ "orgDefinedId"' + ':' + '"' + person.Id + '" }');

Solution

  • I thought I had to pass the parameters as a JSON string in the request body, but actually I need to put them on the URL (the endpoint string), so it's working now