Hi Everyone! Has anyone tried to create a restlet...
# suitescript
n
Hi Everyone! Has anyone tried to create a restlet to connect with Excel? can you direct me to any documentation about it? Thanks!
n
How fun. Are you wanting to post data from excel to your restlet i assume?
n
Exactly!
n
Neat! So if you are doing it all through the excel file, you can use VBA (excels scripting language) to do an HTTPS request to your restlet endpoint. You would use vba to gather up all the data you want to send. Put it in CSV format. Then POST it to the restlet. Your restlet then has the logic to take the csv data and do something with it. A sample would look something like this
Copy code
Sub PostDataToNetSuiteRESTlet()
    Dim URL As String
    Dim Request As Object
    Dim Response As String
    Dim AuthHeader As String
    Dim PostData As String
    
    ' Set the RESTlet endpoint URL
    URL = "<https://your.netsuite.account.com/app/site/hosting/restlet.nl?script=123&deploy=1>"
    
    ' Set the authentication header
    AuthHeader = "Authorization: NLAuth nlauth_account=youraccount, nlauth_email=youremail, nlauth_signature=yoursignature, nlauth_role=3"
    
    ' Set the data to be posted in CSV format
    ' Replace "A1:B5" with the range of cells containing your data
    PostData = Range("A1:B5").Value
    
    ' Create the HTTP request object
    Set Request = CreateObject("WinHTTP.WinHTTPRequest.5.1")
    
    ' Set the request settings
    Request.Open "POST", URL, False
    Request.SetRequestHeader "Content-Type", "text/csv"
    Request.SetRequestHeader "Authorization", AuthHeader
    Request.Send PostData
    
    ' Handle the response
    Response = Request.responseText
    ' Do something with the response data
End Sub
w
message has been deleted
🤣 4
n
holy shit @Watz you totally caught me lol
😂 2
w
I mean user credentials and RESTlets doesn’t work anymore, and the wording really felt like ChatGPT
But it’s probably a good start. Ask it to replace the nlauth stuff with OAuth authentication that works with Netsuite.
n
Copy code
Sub PostDataToNetSuiteRESTletWithOAuth()
    Dim URL As String
    Dim Request As Object
    Dim Response As String
    Dim ConsumerKey As String
    Dim ConsumerSecret As String
    Dim AccessToken As String
    Dim AccessTokenSecret As String
    Dim OAuthHeader As String
    Dim PostData As String
    
    ' Set the RESTlet endpoint URL
    URL = "<https://your.netsuite.account.com/app/site/hosting/restlet.nl?script=123&deploy=1>"
    
    ' Set the OAuth credentials
    ConsumerKey = "your_consumer_key"
    ConsumerSecret = "your_consumer_secret"
    AccessToken = "your_access_token"
    AccessTokenSecret = "your_access_token_secret"
    
    ' Set the data to be posted in CSV format
    ' Replace "A1:B5" with the range of cells containing your data
    PostData = Range("A1:B5").Value
    
    ' Create the HTTP request object
    Set Request = CreateObject("WinHTTP.WinHTTPRequest.5.1")
    
    ' Set the request settings
    Request.Open "POST", URL, False
    
    ' Set the OAuth headers
    OAuthHeader = GetOAuthHeader(Request, ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret)
    Request.SetRequestHeader "Authorization", OAuthHeader
    Request.SetRequestHeader "Content-Type", "text/csv"
    
    ' Set the post data
    Request.Send PostData
    
    ' Handle the response
    Response = Request.responseText
    ' Do something with the response data
End Sub

Function GetOAuthHeader(Request As Object, ConsumerKey As String, ConsumerSecret As String, AccessToken As String, AccessTokenSecret As String) As String
    Dim OAuth As Object
    Set OAuth = CreateObject("MSXML2.XMLHTTP")
    
    ' Set the OAuth parameters
    OAuth.Open "GET", "<https://oauth.netsuite.com/oauth/signature>", False
    OAuth.setRequestHeader "Authorization", "OAuth realm=""" & Request.URL & """"
    OAuth.setRequestHeader "oauth_consumer_key", ConsumerKey
    OAuth.setRequestHeader "oauth_nonce", CStr(Now())
    OAuth.setRequestHeader "oauth_signature_method", "HMAC-SHA1"
    OAuth.setRequestHeader "oauth_timestamp", CStr(DateDiff("s", "01/01/1970 00:00:00", Now()))
    OAuth.setRequestHeader "oauth_token", AccessToken
    OAuth.setRequestHeader "oauth_version", "1.0"
    
    ' Generate the OAuth signature
    Dim SignatureBase As String
    SignatureBase = OAuthSignatureBase(Request, OAuth)
    Dim SigningKey As String
    SigningKey = ConsumerSecret & "&" &
Nice
w
Should be SHA256 as SHA1 is going to be deprecated.
s
I don't think ChatGPT makes any claims about the quality of code answers. What I've seen so far is shite.
I fear we'll see an explosion of poor code due to the likes of chatGPT
😅 1
which unfortunately, problably feeds back into itself for the chatGPT learning?
r
computers are stupid