Download a Java Application to Connect to NetSuite OAuth TBA Restlet Endpoints

This aricle is relevant if you are wish to connect to NetSuite RESTLET technology using token based authentication (TBA) using Java as a client.

Background

In general, my team and I perform NetSuite REST and Web Services development using the Postman http test utility. However, we have found that Postman may have challenges with OAuth 1.0 backing NetSuite’s token based authentication. Even when using Postman, you may find that even after knowing you have your credentials right, it still may be challenging to craft the HTTP oAuth 1.0 headers in the client programming environment.

As discussed in my article, Download a NetSuite OAuth Token Based Authentication Sample Node.js Program, I offered a way to connect and test your Token Based Authentication client use Node.js. This article offers effectively the same solution but instead uses Java.

NetSuite Java Class to Connect to REST

Here is the Java Class that will connect up to a NetSuite RESTLet service. It leverages the utilities of the scribejava open source framework.

package prolecto.netsuite.cookbook.tba;

//leverage services of scribejava
import com.github.scribejava.core.builder.api.DefaultApi10a;
import com.github.scribejava.core.model.OAuth1AccessToken;
import com.github.scribejava.core.model.OAuth1RequestToken;
import com.github.scribejava.core.model.OAuthConfig;
import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Verb;
import com.github.scribejava.core.oauth.OAuth10aService;

public class CookbookTBA {

	//modify consumer and token keys as needed.
	private static final String CONSUMER_KEY = "596f458a5fdef2e557825216d6c202fabb1a653c7e2c60339d015d09ca89862c";
	private static final String CONSUMER_SECRET = "da3cc4d398c381033d04979b584c4b12498c15c61ee6bfc49447768875d9741e";
	private static final String TOKEN_KEY = "9af196bd474a1d2c50b80cc8389b8e8cb7f7bfd72833a37643696b1f74da8d32";
	private static final String TOKEN_SECRET = "cca7cd1dd20b88e9d54a3cac3a62273431c591ec068ec64007cdd927319718bd";
	
	public static void main(String[] args) throws Exception {
		System.out.println("Test: NetSuite Token Based Authentication...");
		doTbaCall();
		System.out.println("Test: Done!");
	}
	
	private static void doTbaCall() throws Exception {
	    OAuthConfig authConfig = new OAuthConfig(CONSUMER_KEY, CONSUMER_SECRET);
	    OAuth1AccessToken token = new OAuth1AccessToken(TOKEN_KEY, TOKEN_SECRET);
	    try (OAuth10aService auth10aService = new OAuth10aService(new NSApi(), authConfig)) {
		    
		    //modify NetSuite endpoint 
		    String url = "https://rest.na1.netsuite.com/app/site/hosting/restlet.nl?script=479&deploy=1";
	    	    //indicate HTTP Verb
	    	    OAuthRequest request = new OAuthRequest(Verb.POST, url);
		    //set the account
		    request.setRealm("TSTDRV1030358");
		    request.addHeader("Content-Type", "application/json");
		    request.setPayload("{\"message\" : \"Hello World\"}");
		    System.out.println("PAYLOAD:");
		    System.out.println(request.getStringPayload());
		    auth10aService.signRequest(token, request);
		    System.out.println("RESULT:");
		    Response response = auth10aService.execute(request);
		    System.out.println(response.getBody());
	    }
	}
}

//this class is a non functional stub required by the scribejava framework
class NSApi extends DefaultApi10a {

	@Override
	public String getRequestTokenEndpoint() {
		return null;
	}

	@Override
	public String getAccessTokenEndpoint() {
		return null;
	}

	@Override
	public String getAuthorizationUrl(OAuth1RequestToken requestToken) {
		return null;
	}
	
}

Download Java Client TBA Program to Connect to NetSuite REST Services

You can download this program here. To test the program, assuming you have Java installed, get the base directory of the unzipped program and enter this command at your command prompt. It should run without compiling. However, if you modify the java program, as you should for testing your specific endpoint, you will need recompile the program before you execute.

Linux / Mac: java -cp bin/:lib/scribejava-core-4.2.0.jar prolecto.netsuite.cookbook.tba.CookbookTBA

Windows: java -cp bin/;lib/scribejava-core-4.2.0.jar prolecto.netsuite.cookbook.tba.CookbookTBA

Work with Fellow NetSuite Experts

My goal in this article was to help fellow NetSuite software developers lower the learning curve to more quickly become productive. If you have perused a number of our articles, you have found that we love inventing with the NetSuite platform. If you have passion for NetSuite as we do, perhaps you can join our team so we can help each other bring innovation to the community.

Be Sociable, Share!

Marty Zigman

Holding all three official certifications, Marty is Southern California's NetSuite expert and leads a team of senior professionals at Prolecto Resources, Inc. He is a former Deloitte & Touche CPA and has held CTO roles. For over 25 years, Marty has produced leadership in ERP, CRM and eCommerce business systems. Contact Marty to setup a conversation.

More Posts - Website - Twitter - Facebook - LinkedIn - Google Plus - YouTube

| Category: NetSuite, Technical | 1 Comment

One Comment

  1. Gabriel
    Posted November 12, 2017 at 11:01 pm | Permalink

    Two thumbs up! Thank you for code sharing.
    I wish NetSuite would be implementing OAuth 2.0, but according to enhancement requests, NetSuite is not considering this at present or in the near future.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>