Skip to content

getting 401 while authenticating. #307

@archana-codezen

Description

@archana-codezen

I am using the following code :

require 'oauth'
require 'net/http'
require 'uri'
require 'json'
require 'securerandom'
require 'openssl'

class SchemeDataImporter
  attr_reader :script_id, :deploy_id, :realm, :consumer, :token

  def initialize(script_id, deploy_id, realm, consumer_key, consumer_secret, token, token_secret)
    @script_id = script_id
    @deploy_id = deploy_id
    @realm = realm
    @token_secret = token_secret
    @callback_url = "https://#{realm}.xyz.netsuite.com/app/site/hosting/xyz.nz?script=#{script_id}&deploy=#{deploy_id}"
    @consumer = OAuth::Consumer.new(consumer_key, consumer_secret, {
      site: "https://#{realm}.xyz.netsuite.com", 
      signature_method: 'HMAC-SHA256',
      oauth_version: '1.0' 
    })

    @token = OAuth::AccessToken.new(@consumer)
    
  end

  def import_data
    url = "/app/site/hosting/xyz.nz?script=#{script_id}&deploy=#{deploy_id}"
    
    # Generate OAuth header
    auth_header = generate_auth_header(url)
    response = @token.request(:get, "/app/site/hosting/xyz.nz?script=#{OAuth::Helper.escape(script_id)}&deploy=#{OAuth::Helper.escape(deploy_id)}")
    # response = @token.get(url, {
    #   'Authorization' => auth_header,
    #   'Content-Type' => 'application/json',
    #   'Accept' => 'application/json'
    # })


    puts "-------------------------------------"
    puts "-------------------------------------"
    puts auth_header
    puts realm
    puts script_id
    puts deploy_id

    puts "-------------------------------------"
    puts "-------------------------------------"



    puts "Response Code: #{response.code}"
    puts "Response Message: #{response.message}"
    puts "Response Body: #{response.body}"
  
    if response.code.to_i == 200
      # Handle successful response
      puts "Import process complete"
    else
      raise "Failed to fetch data: #{response.code} #{response.message} - #{response.body}"
    end
  end

  private

  def generate_auth_header(url)
    oauth_params = {
      oauth_consumer_key: @consumer.key,
      oauth_token: @token.token,
      oauth_signature_method: @consumer.options[:signature_method],
      oauth_timestamp: (Time.now.to_i + 120).to_s,
      oauth_nonce: generate_nonce,
      oauth_version: '1.0',
      realm: @realm
    }

    base_string = generate_base_string(url, oauth_params)
    puts "Base String: #{base_string}" # Log base string for debugging
  
    oauth_signature = generate_signature(base_string)
    puts "OAuth Signature: #{oauth_signature}" # Log the generated signature
  
    oauth_params[:oauth_signature] = oauth_signature
    auth_header = "OAuth " + oauth_params.map { |key, value| "#{key}=\"#{URI.encode(value.to_s)}\"" }.join(", ")
  
    puts "Authorization Header: #{auth_header}" # Log the final auth header
    auth_header
    
  end

  def generate_nonce
    # Generate a random number between 0 and 99999999999 (11 digits)
    rand(10**11).to_s.rjust(11, '0')
  end
  

  def generate_base_string(url, oauth_params)
    sorted_params = oauth_params.sort.to_h
    param_string = sorted_params.map { |k, v| "#{k}=#{URI.encode(v.to_s)}" }.join("&")

    http_method = "GET"
    base_string = "#{http_method}&#{URI.encode(url)}&#{URI.encode(param_string)}"
    
    base_string
  end

  def generate_base_string(url, oauth_params)
    sorted_params = oauth_params.sort.map { |k, v| "#{k}=#{URI.encode(v.to_s)}" }.join("&")
    http_method = "GET"
    base_string = "#{http_method}&#{URI.encode(url)}&#{URI.encode(sorted_params)}"
    base_string
  end
  
  def generate_signature(base_string)
    key = "#{@consumer.secret}&#{@token.secret}"
    hmac = OpenSSL::HMAC.digest('sha256', key, base_string)
    Base64.strict_encode64(hmac)
  end
end
  

# Example usage
importer = SchemeDataImporter.new(
  "8xx",
  "x",
  "7xxxxxxx",
  "4xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "2xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "7xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "5xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)

begin
  importer.import_data
rescue => e
  puts e.message
end

I am using ruby '2.4.6' & rails '5.2.3'

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions