python-3.xnetsuitesuitetalk

NetSuite python TBA Authentication


I am new to this area of using SOAP for NetSuite calls. Hence, I might be thinking completely incorrectly on how to solve the problem. Here is what I am trying to solve: - Language: Python+ Zeep - I want to move my application from email pass to Token based authentication.

In Python I am able to generate all the parameters for TokenPassport. Here is where I am confused: I looked up some code on stack and noticed that folks were using client.service.login() method to login. This method takes the passport and not the tokenpassport obj. Is there a separate method that takes tokenpassport obj for login?, Or do I need to generate(hardcode) an XML with the parameters and is this passed in header as data?

Thanks T


Solution

  • Hope the below code helps someone who is starting out.

    base = '&'.join([nsAccountID, consumerKey, token, Nonce, currentTime])
    key = '&'.join([consumerSecret, tokenSecret])
    digest = hmac.new(str.encode(key), msg=str.encode(base), digestmod=hashlib.sha256).digest()
    signature = base64.b64encode(digest).decode()
    
    tokenPassport = client.get_type('ns0:TokenPassport')
    PassportSignature = client.get_type('ns0:TokenPassportSignature')
    tokenPassportSignature = PassportSignature(signature, "HMAC-SHA256" )
    clientPass = tokenPassport(account=nsAccountId, consumerKey = consumerKey, token= token, nonce= Nonce, timestamp=currentTime, signature=tokenPassportSignature)
    
    
    search = client.get_type('ns5:ItemSearchBasic')
    searchCriteriaComplex = client.get_type('ns0:SearchStringField')
    searchCriteria = searchCriteriaComplex(searchValue= "Test Display Name - tax", operator="is")
    searchItem = search(displayName = searchCriteria)
    testRes = client.service.search(searchRecord= searchItem, _soapheaders={"tokenPassport": clientPass})