Troubleshooting 404 Error in QR Code Generation for Android Mana

ghz 8months ago ⋅ 65 views

Troubleshooting 404 Error in QR Code Generation for Android Management API Enrollment Token

I have developed a Mobile Device Management application using the Google Android Management API in Node.js. The application was working properly, but now I am facing a 404 not found error when I create an enrollment token with the QR code. Can you suggest a solution to resolve this issue?

here is the code

const createEnrollmentToken = async (req,res,policyId) => {
    try {
        if (global.collectionExists) {
            console.log(`Collection already exists. Data will not be stored.`);
        } else {
            
            // Qr Code Exapraition time setting for 1000 years
            const durationInYears = 1000;
            const secondsInYear = 365.25 * 24 * 60 * 60; // 365.25 days in a year to account for leap years
            const durationInSeconds = durationInYears * secondsInYear;
            const enrollmentToken = await androidManagementClient.enterprises.enrollmentTokens.create({
                parent: global.enterpriseName,
                requestBody: {
                    policyName: policyId,
                    duration: `${durationInSeconds}s`,
                }
            });
            console.log('Enrollment token created successfully:', enrollmentToken.data);
            try {
                /* qr image  */
                const policies = await PoliciesDataModel.find({ username: global.adminUserName, "Polices.policyName": req.body.policyId });
                if (policies && policies.length > 0) {
                    const image = {
                        'cht': 'qr',                                      //cht represents the type of the chart or image, and its value is set to 'qr'.
                        'chs': '320x320',                                //chs specifies the size of the chart or image, and its value is set to '500x500', indicating a width and height of 500 pixels each.
                        'chl': enrollmentToken.data.qrCode               //chl is the URL or data for the chart or image, and its value is taken from enrollmentToken.data.qrCode.
                    };
                    console.log(enrollmentToken.data.qrCode  )
                    const qrcodeImgUrl = 'https://chart.googleapis.com/chart?' + querystring.stringify(image);
                    console.log('qrcodeImgUrl : ', qrcodeImgUrl)
                    const qrDoc = {
                        QrcodeImg: qrcodeImgUrl,
                        expiredDateTime: enrollmentToken.data.expirationTimestamp
                    };

                } else {
                    // If no policies were found, send a not found response
                    console.log('No policies found for the given email and policy name.');
                    await res.redirect('/provision-Template');
                }
            } catch (error) {
                console.log("Update Error", error)
            }
            // expiredDateTime: enrollmentToken.data.expirationTimestamp
            await res.redirect('/provision-Template');

        
        }
    } catch (error) {
        console.error('Error creating enrollment token:', error.message);
    }
};

References : - https://colab.research.google.com/github/google/android-management-api-samples/blob/master/notebooks/quickstart.ipynb#scrollTo=TTSgnpC9EScx

enter image description here

Answers

The 404 not found error you are encountering when creating an enrollment token with the QR code suggests that the URL you are trying to access is not valid or cannot be found. In this case, the URL is generated using the Google Chart API to create a QR code image.

To resolve this issue, you can try the following steps:

  1. Verify the Generated QR Code URL: Log the generated QR code URL (qrcodeImgUrl) and verify if it is formed correctly. You can print it to the console using console.log('qrcodeImgUrl : ', qrcodeImgUrl).

  2. Test the QR Code URL: Copy the generated QR code URL from the console and paste it into a web browser to see if the QR code image is displayed correctly. If the image is not displayed or if you encounter a 404 error, there might be an issue with the URL generation or the Google Chart API.

  3. Check Google Chart API Documentation: Verify if there are any changes or deprecations in the Google Chart API that might be causing the issue. Make sure you are using the correct parameters and syntax for generating the QR code URL.

  4. Use a Different QR Code Generator: If the issue persists, consider using a different QR code generation service or library to generate the QR code image. There are several third-party libraries and APIs available for generating QR codes.

  5. Handle Errors Properly: Make sure to handle errors gracefully in your code. If an error occurs during the QR code generation process, log the error message and handle it appropriately to provide a meaningful response to the user.

By following these steps and verifying the QR code generation process, you should be able to identify and resolve the 404 not found error when creating the enrollment token with the QR code.