Sunday, April 14, 2019

RESTLet - URL Encoding for Authentication Password Character – Java (Prevents 401: Error)

When doing Authentication Password, it is good to do URL Encoding for Authentication Password (or nlauth_signature). The reason is, sometime users would have special characters in there password, and NetSuite RESTLet authentication does not accept Special characters unless it is URL Encoded. The following error will then occur: 401 Authorizations Required: INVALID_LOGIN_CREDENTIALS if the correct password has no URL Encoding for Special Characters.

Therefore, this article aims to show the function that can used to do Url Encoding in Java.

Library: java.lang.Object
Class: URLEncoder
extends Object
Method: encode(String s)

For more information on this function/class go to: Class URLEncoder - http://docs.oracle.com/javase/7/docs/api/java/net/URLEncoder.html

Sample:

URLEncoder uRLEncoder = new URLEncoderencodedPassword = uRLEncoder.encode(password);// password would be a string variable that would have been initialized before. URL url = new URL(https://rest.netsuite.com/app/site/hosting/restlet.nl?script=83&deploy=1);//url of the RESTlet that can be found in the deployment of the RESTlet script HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection(); urlConn.setRequestMethod("GET"); //set GET method urlConn.setAllowUserInteraction(false); urlConn.setDoOutput(true); urlConn.setRequestProperty( "Content-type", "application/json" );// set Content type urlConn.setRequestProperty( "Authorization", "NLAuth nlauth_account=accountid,nlauth_email=email@netsuite.com,nlauth_signature=" + encodedPassword + ", nlauth_role=3" );// set credentials.

No comments:

Post a Comment