Python requests - invalid paramaters on POST request for record list

Hiya,

I have split this up into what I am trying to do and the issue it's self  for people who wanna jump straight to the issue or want to read on what I

am trying to do first.

what I am trying to do:

I have been recently making a program for our work to intergrate our product stock to our website from crm, upon searching around in SugarCRM I noticed a convinient tool that allowed us to export all of our products (ProductTemplates) into an excel file through a POST request, I replicated this post request in python requests which worked great however I was confused when it all of a sudden didn't work to find that the link (specifically the ID) of the document kept changing, upon seeing this I re analysed for different options and noticed there was a POST request for a "record list" for all of our products which contains requests paramaters for a set of ID's (which I am guessing are sets of products) which returns from the server a single ID to export the excel file

the issue:

The Issue I am having is that when I try to use a post request to get the "record_list" which contains the Main ID needed to get all of our product templates to export them I get the error "No valid authentication for user" even when I have provided the cookie that was given at the time of logging in, I have also tried sending over the login payload again with the following payload {"username": "hidden", "password": "hidden", "client_id": "sugar", "grant_type": "hidden" "client_secret": hidden,"platform": "base"} having no luck with it still saying there is no valid authentication for user (note I am certain the token has not expired as the program it's self takes 10 seconds from login to POST request for record list), this leads me to believe that there is paramaters that I am msising or that are just wrong. If you have any ideas on what I am doing wrong, please let me know.

Thank you for taking your time to read my question.

Parents
  • Hiya Francesca,

    I am able to grab the Oauth token saved which is saved in a text file upon logging in, I am also using sessions to hold the cookies for me which you can see in the code below.

    payload 1 = {hidden}
    
    with requests.session() as a:
        response = a.post('.../rest/v11_5/oauth2/token', data=payload)
        response.raise_for_status()
        file = open(r"auth.txt", "w")
        #opens auth text file (overwrites if file already exists) in write mode
        file.write(response.text)
        # writes response from OAUTH request into auth.txt
        file.close()
        # File close
        file1 = open(r"auth.txt", "r")
        #opens auth.txt in read mode
        authcopy = open(r"authcopy.txt", "r+t")
        #opens authcopy.text in write and read mode
        authcopy.write(file1.readline(54).replace('{"access_token":', ''))
    #writes to authcopy the OAUTH key with nothing else included (token type, refresh token etc)
    
    payload 2 = {hidden}
    
    ### this is the part that had the issue
    
    ID = a.post('../rest/v11_5/ProductTemplates/record_list', data=payload2)
        print(ID.text) ### just to test it's working



    I am not very familiar with the REST API and modules so I'll have to do some reading of the documentation, if there is anything else that
    you think I should know about to help me in my quest, please let me know. Thank you for your help!
  • Hi Thomas,

    You're missing a Bearer/OAuth header in your request, it cannot be a payload.

    Try this.

    Headers = { “OAuth-Token” : ”our_unique_secret_token” }
    
    response = request.post(“https://surgar_url”, headers=Headers)

    SugarCRM | Principal Developer Advocate

Reply Children
No Data