{"id":333,"date":"2022-08-30T05:49:36","date_gmt":"2022-08-30T05:49:36","guid":{"rendered":"https:\/\/simplecode.xyz\/?p=333"},"modified":"2022-08-30T05:49:37","modified_gmt":"2022-08-30T05:49:37","slug":"oauth-1-signature-rails-netsuite-example","status":"publish","type":"post","link":"https:\/\/simplecode.com.vn\/?p=333","title":{"rendered":"OAuth 1 Signature &#8211; Rails + NETSUITE example"},"content":{"rendered":"\n<p>When we decide to use Oauth 1 for authentication, we need to answer the first question: &#8216;How is the signature generated?&#8217;. It took me 3 days to work it out before I could get data from our client&#8217;s NETSUITE account.<\/p>\n\n\n\n<p>I want to share with you my first raw Ruby code and hope it will save you a bit of time.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># NETSUITE Account vars\nclient_key = 'xxx'\nclient_secret = 'xxx'\ntoken_id = 'xxx'\ntoken_secret = 'xxx'\naccount_id = 'xxx'\n\n# OAuth vars\noauth_signature_method = 'HMAC-SHA256'\noauth_timestamp = Time.now.to_i\noauth_nonce = ERB::Util.url_encode(SecureRandom.base64)\nhttp_request_method = 'POST' #'GET'\nversion = '1.0'\n\n# Init url\nurl_text = \"https:\/\/#{account_id.downcase}.suitetalk.api.netsuite.com\/services\/rest\/query\/v1\/suiteql\"\n# URL params\nparams = { limit: 5 }\nparams = {}\n# We need to percent encode URL params\nparams_text = params.map {|k, v| \"#{k}=#{ERB::Util.url_encode(v.to_s)}\" }.join(\"\\&amp;\")\nurl = URI(url_text)\nurl.query = params_text\n\n# Init request parameters for signature base string\n# The key name must be in alphabet order\nrequest_params = {\n  oauth_consumer_key: client_key,\n  oauth_nonce: oauth_nonce,\n  oauth_signature_method: oauth_signature_method,\n  oauth_timestamp: oauth_timestamp,\n  oauth_token: token_id,\n  oauth_version: version\n}\n\nrequest_params_ordered = request_params.dup\n\n# We need to put all URL params to the request parameters of the signature base string\nparams.each do |k, v|\n  request_params_ordered&#91;k] = ERB::Util.url_encode(v.to_s) # Percent encode\nend\n\nrequest_params_ordered = request_params_ordered.sort_by { |k, _| k }\nnormalized_request_parameters = request_params_ordered.map {|k, v| \"#{k}=#{v.to_s}\" }.join(\"\\&amp;\")\n# Init base string for signature\nbase_string = \"#{http_request_method}\\&amp;#{ERB::Util.url_encode(url_text)}\\&amp;#{ERB::Util.url_encode(normalized_request_parameters)}\"\n# Init secret key for signature\nsecret_key = \"#{client_secret}\\&amp;#{token_secret}\"\n# Generate signature\nhmac_signature = ERB::Util.url_encode(Base64.strict_encode64(OpenSSL::HMAC.digest('sha256', secret_key, base_string)))\n\n# Init Authorization header\nauthorization = &#91;\n  \"realm=\\\"#{account_id.upcase}\\\"\",\n  \"oauth_consumer_key=\\\"#{client_key}\\\"\",\n  \"oauth_token=\\\"#{token_id}\\\"\",\n  \"oauth_signature_method=\\\"#{oauth_signature_method}\\\"\",\n  \"oauth_timestamp=\\\"#{oauth_timestamp}\\\"\",\n  \"oauth_nonce=\\\"#{oauth_nonce}\\\"\",\n  \"oauth_version=\\\"#{version}\\\"\",\n  \"oauth_signature=\\\"#{hmac_signature}\\\"\"\n].join(',')\n\nrequest_body = {\n  q: \"SELECT * FROM account\"\n}\n\nhttps = Net::HTTP.new(url.host, url.port)\nhttps.use_ssl = true\nmethod_class_name = \"Net::HTTP::#{http_request_method.titlecase}\".constantize\nrequest = method_class_name.new(url)\n# request = Net::HTTP::Post.new(url)\n# request = Net::HTTP::Get.new(url)\nrequest&#91;\"Authorization\"] = \"OAuth #{authorization}\"\nrequest&#91;\"prefer\"] = \"transient\"\nrequest&#91;\"Content-Type\"] = \"application\/json\"\nrequest.body = JSON.dump(request_body)\n\nresponse = https.request(request)\nputs response.read_body<\/code><\/pre>\n\n\n\n<p>Happy coding!<\/p>\n\n\n\n<p>References: https:\/\/developer.twitter.com\/en\/docs\/authentication\/oauth-1-0a\/creating-a-signature<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we decide to use Oauth 1 for authentication, we need to answer the first question: &#8216;How is the signature generated?&#8217;. It took me 3 days to work it out before I could get data from our client&#8217;s NETSUITE account. I want to share with you my first raw Ruby [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v17.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>OAuth 1 Signature - Rails + NETSUITE example - SimpleCode<\/title>\n<meta name=\"description\" content=\"authenticate oauth1 signature netsuite token ruby rails\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/simplecode.com.vn\/?p=333\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"OAuth 1 Signature - Rails + NETSUITE example - SimpleCode\" \/>\n<meta property=\"og:description\" content=\"authenticate oauth1 signature netsuite token ruby rails\" \/>\n<meta property=\"og:url\" content=\"https:\/\/simplecode.com.vn\/?p=333\" \/>\n<meta property=\"og:site_name\" content=\"SimpleCode\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-30T05:49:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-08-30T05:49:37+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"simplecode\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/simplecode.com.vn\/#website\",\"url\":\"https:\/\/simplecode.com.vn\/\",\"name\":\"SimpleCode\",\"description\":\"Simple Code\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/simplecode.com.vn\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/simplecode.com.vn\/?p=333#webpage\",\"url\":\"https:\/\/simplecode.com.vn\/?p=333\",\"name\":\"OAuth 1 Signature - Rails + NETSUITE example - SimpleCode\",\"isPartOf\":{\"@id\":\"https:\/\/simplecode.com.vn\/#website\"},\"datePublished\":\"2022-08-30T05:49:36+00:00\",\"dateModified\":\"2022-08-30T05:49:37+00:00\",\"author\":{\"@id\":\"https:\/\/simplecode.com.vn\/#\/schema\/person\/b110785905231d29553717dd14b766dc\"},\"description\":\"authenticate oauth1 signature netsuite token ruby rails\",\"breadcrumb\":{\"@id\":\"https:\/\/simplecode.com.vn\/?p=333#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/simplecode.com.vn\/?p=333\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/simplecode.com.vn\/?p=333#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/simplecode.com.vn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"OAuth 1 Signature &#8211; Rails + NETSUITE example\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/simplecode.com.vn\/#\/schema\/person\/b110785905231d29553717dd14b766dc\",\"name\":\"simplecode\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/simplecode.com.vn\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/22e0b2cc28939e5aecc166195d629442?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/22e0b2cc28939e5aecc166195d629442?s=96&d=mm&r=g\",\"caption\":\"simplecode\"},\"url\":\"https:\/\/simplecode.com.vn\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"OAuth 1 Signature - Rails + NETSUITE example - SimpleCode","description":"authenticate oauth1 signature netsuite token ruby rails","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/simplecode.com.vn\/?p=333","og_locale":"en_US","og_type":"article","og_title":"OAuth 1 Signature - Rails + NETSUITE example - SimpleCode","og_description":"authenticate oauth1 signature netsuite token ruby rails","og_url":"https:\/\/simplecode.com.vn\/?p=333","og_site_name":"SimpleCode","article_published_time":"2022-08-30T05:49:36+00:00","article_modified_time":"2022-08-30T05:49:37+00:00","twitter_card":"summary","twitter_misc":{"Written by":"simplecode","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/simplecode.com.vn\/#website","url":"https:\/\/simplecode.com.vn\/","name":"SimpleCode","description":"Simple Code","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/simplecode.com.vn\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/simplecode.com.vn\/?p=333#webpage","url":"https:\/\/simplecode.com.vn\/?p=333","name":"OAuth 1 Signature - Rails + NETSUITE example - SimpleCode","isPartOf":{"@id":"https:\/\/simplecode.com.vn\/#website"},"datePublished":"2022-08-30T05:49:36+00:00","dateModified":"2022-08-30T05:49:37+00:00","author":{"@id":"https:\/\/simplecode.com.vn\/#\/schema\/person\/b110785905231d29553717dd14b766dc"},"description":"authenticate oauth1 signature netsuite token ruby rails","breadcrumb":{"@id":"https:\/\/simplecode.com.vn\/?p=333#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/simplecode.com.vn\/?p=333"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/simplecode.com.vn\/?p=333#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/simplecode.com.vn\/"},{"@type":"ListItem","position":2,"name":"OAuth 1 Signature &#8211; Rails + NETSUITE example"}]},{"@type":"Person","@id":"https:\/\/simplecode.com.vn\/#\/schema\/person\/b110785905231d29553717dd14b766dc","name":"simplecode","image":{"@type":"ImageObject","@id":"https:\/\/simplecode.com.vn\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/22e0b2cc28939e5aecc166195d629442?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/22e0b2cc28939e5aecc166195d629442?s=96&d=mm&r=g","caption":"simplecode"},"url":"https:\/\/simplecode.com.vn\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/simplecode.com.vn\/index.php?rest_route=\/wp\/v2\/posts\/333"}],"collection":[{"href":"https:\/\/simplecode.com.vn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simplecode.com.vn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simplecode.com.vn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/simplecode.com.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=333"}],"version-history":[{"count":5,"href":"https:\/\/simplecode.com.vn\/index.php?rest_route=\/wp\/v2\/posts\/333\/revisions"}],"predecessor-version":[{"id":338,"href":"https:\/\/simplecode.com.vn\/index.php?rest_route=\/wp\/v2\/posts\/333\/revisions\/338"}],"wp:attachment":[{"href":"https:\/\/simplecode.com.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simplecode.com.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simplecode.com.vn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}