Looking for a very simple example of REST API conn...
# suitetalkapi
e
Looking for a very simple example of REST API connection. Does anyone knows a Github project or blogpost with a simple, single-script example of REST API usage using Java? I found some RESTlet and SOAP examples, but nothing to REST API.
👀 1
c
Do you know java? REST is easy using spring boot
s
The hardest part is really the auth token, and generating that will be dependent on the language you're writing for. Once you get that down the rest is pretty well documented/easy
c
@slc-abotbyl OP said Java
👍 1
s
Need more coffee 🙂
e
Yep, the auth thing is the part I can't get going.
l
@Edgar Valdes, have you tried using postman first? they have postman collections ready you can use them to integrate, once that is successful you can copy the sample code from the postman it self.
What type of Auth flow you need? I mean TBA, OAuth 2.0- Authorization Code Grant or Client Credentials(Machine to Machine)?
I can help a a bit to setup REST in postman..
e
Postman works fine. But I can't get the TBA auth working in Java (trying to generate the signature)
It's a real stopper. If a TBA auth library for NetSuIte REST API would exist, I could concentrate in the meat of the application 🙂 Maybe I could search in another language, if such helper library already exists for NetSuite.
s
I do this in node, not sure if that would help you?
e
Sure! @slc-abotbyl
s
👍 I'll throw it in here in a bit.
thanks 1
Ah, actually most of the heavy lifting for NS is done by the OAuth node module, but this may help a bit...
Copy code
const oauth = new OAuth({
      consumer: {
        key: this.config.consumerKey,
        secret: this.config.consumerSecret,
      },
      realm: this.config.account,
      signature_method: 'HMAC-SHA256',
      hash_function: (baseString, key) => {
        return crypto
          .createHmac('sha256', key)
          .update(baseString)
          .digest('base64');
      },
    });

    this.rest.interceptors.request.use((config) => {
      /**
       * Add OAuth Headers to the request.
       */
      config.url = decodeURI(this.rest.getUri(config)).replace(/\+/g, ' ');
      config.params = {};
      const OAuthHeaders = oauth.toHeader(
        oauth.authorize(
          {
            includeBodyHash: true,
            url: config.url,
            method: config.method || 'get',
            data: config.data,
          },
          {
            key: this.config.tokenId,
            secret: this.config.tokenSecret,
          },
        ),
      );
      config.headers['Authorization'] = OAuthHeaders.Authorization;
      return config;
    });
🙌 1
e
Thanks @slc-abotbyl!!!