• Resolved mhmdsalah

    (@mhmdsalah)


    I’m trying to use litfer-lms as a headless with my next.js application. I want to enrol users grammatically, the problem is when I make the request, I get:

    code: 'llms_rest_unauthorized_request',
    message: 'Sorry, you are not allowed to create an enrollment as this user.',
    function getAuthHeader() {
    	const username = process.env.LIFTER_LMS_CONSUMER_KEY as string;
    	const password = process.env.LIFTER_LMS_CONSUMER_SECRET as string;
    	const authString = ${username}:${password};
    	const base64AuthString = Buffer.from(authString).toString("base64");
    	return Basic ${base64AuthString};
    }
    
    class LMSHeadlessAPI {
    	/**
    	 * Axios instance for making HTTP requests to the LifterLMS Headless API.
    	 */
    	private static api: AxiosInstance = axios.create({
    		baseURL: process.env.NEXT_PUBLIC_WORDPRESS_URL + /wp-json/llms/v1,
    		headers: {
    			Authorization: getAuthHeader(),
    		},
    	});
    
    static async enrollInCourse(params: Enrollment): Promise<void> {
    
    try {
    
    const url = /students/${params.userId}/enrollments/${params.courseId};
    
    const a = await LMSHeadlessAPI.api.post(url);
    
    } catch (err) {
    
    const error = err as AxiosError;
    
    console.log("err", err);
    
    LMSHeadlessAPI.handleError(error);
    
    }
    
    }
    
    
    
    
     

    I added the code above of how I make the request. I maybe the request should include any info about the user? or cookie or something? I don’t know because the doc didn’t say anything about that.

    this is the endpoint I’m using:
    https://developer.lifterlms.com/rest-api/#tag/Student-Enrollments/paths/~1students~1{id}~1enrollments~1{post_id}/post

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter mhmdsalah

    (@mhmdsalah)

    Note, I tried using the Header Authentication also it didn’t work:

    	private static api: AxiosInstance = axios.create({
    		baseURL: process.env.NEXT_PUBLIC_WORDPRESS_URL + /wp-json/llms/v1,
    		headers: {
    			"X-LLMS-CONSUMER-KEY": process.env.LIFTER_LMS_CONSUMER_KEY as string,
    			"X-LLMS-CONSUMER-SECRET": process.env
    				.LIFTER_LMS_CONSUMER_SECRET as string,
    		},
    	});




    Thread Starter mhmdsalah

    (@mhmdsalah)

    This is the curl:

    curl "https://lms.local/wp-json/llms/v1/students/2/enrollments/66" \
      -X POST \
      -H "authorization: Basic Y2tfYmFkNzg1ODg4NDEyNTJmYzU5OTI0MzQ0NWY0YzVmZTcwYzlkMjE1OTpjc184ZDNkYTIzNGIwNGY3MWEyN2QwYWI3MGQ0YjUxN2M2Y2JjNDNmY2Zj" 
    Plugin Support Nick Mariano

    (@reddotinmotion)

    Hi @mhmdsalah,

    When you created you consumer API keys in WordPress Dashboard > LifterLMS > Settings > REST API > API Keys did you set Permissions to Read / Write?

    Thread Starter mhmdsalah

    (@mhmdsalah)

    Yes, I did.

    Thank you for your response, I found the problem which was to request my local using HTTPS, not HTTP.

    I believe lifter-lms shouldn’t enforce https in development or at least the response message should be clear about the problem with the https connections.

    • This reply was modified 8 months, 1 week ago by mhmdsalah.
    Thread Starter mhmdsalah

    (@mhmdsalah)

    simply the curl should be like this:

    curl "https://lms.local/wp-json/llms/v1/students/2/enrollments/66" \
      -X POST \
      -H "authorization: Basic Y2tfYmFkNzg1ODg4NDEyNTJmYzU5OTI0MzQ0NWY0YzVmZTcwYzlkMjE1OTpjc184ZDNkYTIzNGIwNGY3MWEyN2QwYWI3MGQ0YjUxN2M2Y2JjNDNmY2Zj" 

    Plugin Support Nick Mariano

    (@reddotinmotion)

    Hi @mhmdsalah,

    You are correct that the message should be more informative so users could quickly know that they should be using https instead of http. As a result, I created a feature request for that and alerted our developers that the error message should clearly indicate when the non-usage of https is the problem.

    However, even if our developers are already made aware of this, we won’t be able to give you and estimated timeline of when the fix/adjustment/enhancement will be made, tested and released.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to enroll user using the REST API?’ is closed to new replies.