Invalid login attempt while attempting to hit Netsuite SOAP API

243 Views Asked by At

Hi I'm trying to fetch data from NetSuite but unable to do so because of INVALID LOGIN ATTEMPT. Everything is running completely fine on Postman. The code is as following:

import os
import requests
import hashlib
import hmac
import time
import random

# Environment variables
account = "{account_id"
consumer_key = "{consumer key}"
consumer_secret = "{consumer secret}"
token_id = "{token id}"
token_secret = "{token secret}"

# Calculate signature
nonce = str(random.getrandbits(64))
timestamp = str(int(time.time()))
base_string = f"{account}&{consumer_key}&{token_id}&{nonce}&{timestamp}"
key = f"{consumer_secret}&{token_secret}"
signature = hmac.new(key.encode(), base_string.encode(), hashlib.sha256).digest().hex()

# Set environment variables
os.environ["signature"] = signature
os.environ["nonce"] = nonce
os.environ["timestamp"] = timestamp

# Request parameters
url = "https://{account_id}.suitetalk.api.netsuite.com/services/service_id"
headers = {
    "Content-Type": "text/xml",
    "SOAPAction": "get",
    "recordType": "job",
}
body = f"""<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
  <soap-env:Header>
    <tokenPassport>
      <account>{account}</account>
      <consumerKey>{consumer_key}</consumerKey>
      <token>{token_id}</token>
      <nonce>{{nonce}}</nonce>
      <timestamp>{{timestamp}}</timestamp>
      <signature algorithm="HMAC-SHA256">{{signature}}</signature>
    </tokenPassport>
    <preferences>
      <runServerSuiteScriptAndTriggerWorkflows>false</runServerSuiteScriptAndTriggerWorkflows>
    </preferences>
    <searchPreferences>
      <pageSize>1000</pageSize>
      <bodyFieldsOnly>false</bodyFieldsOnly>
    </searchPreferences>
  </soap-env:Header>
  <soap-env:Body>
    <get xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:platformCore="urn:core_2017_1.platform.webservices.netsuite.com">
      <baseRef internalId="4364" type="job" xsi:type="platformCore:RecordRef">
        <platformCore:name/>
      </baseRef>
    </get>
  </soap-env:Body>
</soap-env:Envelope>"""
body = body.format(nonce=nonce, timestamp=timestamp, signature=signature)

# Send request
response = requests.post(url, headers=headers, data=body)

# Print response
print(response.text)

The error being received is - INVALID LOGIN ATTEMPT. It is running completely fine on postman . Seems like something is missing for python. Also, on postman it is returning only one record

1

There are 1 best solutions below

1
AnthoVdo On

You did a mix with the code from Postman and the double {{}}

Your files should look like this:
.env file:

ACCOUNT_ID=123456  
CONSUMER_KEY=yourconsumerkey  
CONSUMER_SECRET=yourconsumersecret  
TOKEN_ID=yourtokenid  
TOKEN_SECRET=yourtokensecret  

Your file should look like that

import os  
import requests  
import hashlib  
import hmac  
import time  
import random  

# Environment variables
account = os.environ["ACCOUNT_ID"]  
consumer_key = os.environ["CONSUMER_KEY"]  
consumer_secret = os.environ["CONSUMER_SECRET"]  
token_id = os.environ["TOKEN_ID"]  
token_secret = os.environ["TOKEN_SECRET"]  

# Calculate signature
nonce = str(random.getrandbits(64))
timestamp = str(int(time.time()))
base_string = f"{account}&{consumer_key}&{token_id}&{nonce}&{timestamp}"
key = f"{consumer_secret}&{token_secret}"
signature = hmac.new(key.encode(), base_string.encode(), hashlib.sha256).digest().hex()

# Set environment variables
os.environ["signature"] = signature
os.environ["nonce"] = nonce
os.environ["timestamp"] = timestamp

# Request parameters
url = "https://{account_id}.suitetalk.api.netsuite.com/services/service_id" => need the service_id  
headers = {
    "Content-Type": "text/xml",
    "SOAPAction": "get", => should reflect what you will add to the envelope
    "recordType": "job", => Not necessary needed
}
body = f"""<soap-env:Envelope 
xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> => need xmlns informations  
  <soap-env:Header>
    <tokenPassport>
      <account>{account}</account>
      <consumerKey>{consumer_key}</consumerKey>
      <token>{token_id}</token>
      <nonce>{nonce}</nonce>
      <timestamp>{timestamp}</timestamp>
      <signature algorithm="HMAC-SHA256">{{signature}}</signature>
    </tokenPassport>
    <preferences>
      <runServerSuiteScriptAndTriggerWorkflows>false</runServerSuiteScriptAndTriggerWorkflows>
    </preferences>
    <searchPreferences>
      <pageSize>1000</pageSize>
      <bodyFieldsOnly>false</bodyFieldsOnly>
    </searchPreferences>
  </soap-env:Header>
  <soap-env:Body>
    <get xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:platformCore="urn:core_2017_1.platform.webservices.netsuite.com"> => you can use 2022_1 for example and should be the same as the service url and the rest of the code  
      <baseRef internalId="4364" type="job" xsi:type="platformCore:RecordRef">
        <platformCore:name/>
      </baseRef>
    </get>
  </soap-env:Body>
</soap-env:Envelope>"""
body = body.format(nonce=nonce, timestamp=timestamp, signature=signature)

# Send request
response = requests.post(url, headers=headers, data=body)

# Print response
print(response.text)

Here is a request to adapt
webservice url:

https://{account}.suitetalk.api.netsuite.com/services/NetSuitePort_2021_1
<soapenv:Envelope
    xmlns:xsd='http://www.w3.org/2001/XMLSchema'
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'
    xmlns:platformCore='urn:core_2021_1.platform.webservices.netsuite.com'
    xmlns:tranSales='urn:sales_2021_1.transactions.webservices.netsuite.com'
    xmlns:platformMsgs='urn:messages_2021_1.platform.webservices.netsuite.com'>
    <soapenv:Header>
        <tokenPassport xsi:type='platformCore:TokenPassport'>
            <account xsi:type='xsd:string'>{ACCOUNT}</account>
            <consumerKey xsi:type='xsd:string'>{CONSUMER_KEY}</consumerKey>
            <token xsi:type='xsd:string'>{TOKEN_ID}</token>
            <nonce xsi:type='xsd:string'>{nonce}</nonce>
            <timestamp xsi:type='xsd:long'>{timestamp}</timestamp>
            <signature algorithm='HMAC_SHA256' xsi:type='platformCore:TokenPassportSignature'>{signature}</signature>
        </tokenPassport>
    </soapenv:Header>
    <soapenv:Body>
        <search xsi:type='platformMsgs:SearchRequest'>
            <searchRecord xsi:type='tranSales:TransactionSearchAdvanced' savedSearchId='1371'/>
        </search>
    </soapenv:Body>
</soapenv:Envelope>

If you have another issue with the login, don't forget to check the login audit trail to understand what went wrong.