Skip to main content

I’m currently encountering an issue while attempting to utilize JWT authentication with the Box SDK in Node.js.



Issue with Invalid ‘sub’ Claim in JWT for Box Authentication(Error: Auth Error: Please check the ‘sub’ claim. The ‘sub’ specified is invalid. d400 Bad Request] invalid_grant - Please check the ‘sub’ claim. The ‘sub’ specified is invalid.)



Specifically, I’m trying to create a folder in my Box account programmatically using the provided code snippet.



Despite verifying the accuracy of the JWT configuration file, ensuring that the JWT token has not expired, and confirming that the client ID and private key information are correct, I continue to encounter authentication errors.



Additionally, I would like to mention that I created the application in the Box Personal Pro version, resulting in an enterprise ID of 0. Could this enterprise ID value be contributing to the authentication issue? Furthermore, even after creating another account with a free version, I’m still experiencing the same authentication error. I have observed that the free account also receives an enterprise ID, albeit still encountering issues with JWT token authentication.



Please Check source code here using Box SDK:



const config = require(‘./0_7c0qhdk1_config.json’);


const BoxSDK = require(‘box-node-sdk’);


const fs = require(‘fs’);


const path = require(‘path’);


// const Folders = require(‘box-node-sdk/lib/managers/folders’);


// const TokenManager = require(‘box-node-sdk/lib/token-manager’);



// Load your JWT configuration file


const configJSON = JSON.parse(fs.readFileSync(‘./0_7c0qhdk1_config.json’));



// Initialize the SDK


const sdk = BoxSDK.getPreconfiguredInstance(configJSON);



// Create a client with JWT authentication


const client = sdk.getAppAuthClient(‘enterprise’);



// Define the folder name and parent folder ID


const folderName = ‘New Folder’;


const parentFolderID = ‘0’; // the root folder



client.folders.create(parentFolderID, folderName)


.then(folder => {


console.log(Folder "${folderName}" created with ID: ${folder.id});


})


.catch(err => {


console.error(‘Error creating folder:’, err);


});



Furthermore, I’ve double-checked the permissions and access levels for my Box account, and everything appears to be in order. However, I’m still unable to authenticate successfully and still gets grant errors.


So And I’ve tried another way.Here are my JWT payload:



const payload = {


iss: ‘CLIENT_ID’,


sub: ‘USER_ID’,


box_sub_type: ‘user’,


aud: ‘https://api.box.com/oauth2/token’,


exp: Math.floor(Date.now() / 1000) + (60 * 60), // Expire in 1 hour


iat: Math.floor(Date.now() / 1000), // Issued at now


nbf: Math.floor(Date.now() / 1000) // Not before now


};


const options = {


algorithm: ‘RS256’, // Box API requires RS256 algorithm


header: {


typ: ‘JWT’


}


};



// Sign the JWT


const token = jwt.sign(payload, privateKey, options);



Despite these checks, I still receive the invalid ‘sub’ claim error. Could you please help me identify what might be causing this issue?


I would greatly appreciate your guidance on how to troubleshoot and resolve this authentication issue. Any insights or suggestions you can provide would be immensely helpful.



Thank you very much for your assistance.



Best regards,

Hi @lu12334



Welcome to the forum, and apologies for the late reply, we’ve been traveling for the last 3 weeks.



You are on the right track, and I don’t se anything wrong with the code samples you supplied.



The issue is your enterprise id 0, in other words you account can’t use a JWT application, because these need approval by your administrator and your account type does not have access to the administration console.



The same situation applies to CCG applications. With you current account all you can use are OAuth applications.



Depending on your use case, you can create a free developer account that will enable you to have any type of application, but it would be a new account so nothing from your current one will be there.



You could also try to modify your script to use OAuth. This is not ideal for a script or application, but if you securely store the refresh token, and use the application at least once every 60 days, you won’t need to keep authorizing it.



I only have a python example of this, so for what is worth you can take a look at this GitHub repo.



Let us know if this helps



Best regards


Reply