Auth problem for below python code

Hi ,

I am getting either an invalid path for (api 1.3) or not authenticated for (api 2.0) to get the transactional API for line items and IDs.

I am using basic auth with base64 encoding.

below is my code, I believe my code is correct. guide me if i am missing anything in the code.

Note that, I have used GUIDs and auth as mguid, wguid, username and password. so the first part is not shared in the below code.


Code

user = 'Basic ' + str(base64.b64encode((

       f'{username}:{password}').encode('utf-8')).decode('utf-8'))



getHeaders = {

    'Authorization': user,

    'Content-Type': 'application/json'

}


getImports = requests.get(f'https://api.anaplan.com/2/0/workspaces/{wGuid}/' +

                        f'models/{mGuid}/lineitems',

                        headers=getHeaders)


with open('lineitems.json', 'wb') as f:

    f.write(getImports.text.encode('utf-8'))


Thanks,

Manjunath

Answers

  • @StevenBeerthuizen @ryan_kohn @kevin.cho anybody can help me understand what is wrong here. Please give me a correct code to execute the transactional API for python.

  • Probably it is somewhere in the user part. Seems to me that you do too much in that code. This is the simple version: user_pw = b64encode(b'email:password').decode()

    The decode already creates an output string so no need to do the str() anyway. I guess you have to play around with it.

    Maybe like this:

    user = 'Basic ' + base64.b64encode((f'{username}:{password}').encode('utf-8')).decode()

    Good luck!

    Steven

  • Hey @ManjunathKN - two issues here:

    1. Steven is correct in that you're doing too much in the user_pw bit.
    2. However, you can't make calls to the transactional/bulk metadata APIs directly using basic authentication like this. Your Authorization header should look like:

    {'Authorization': 'AnaplanAuthToken <token_value>'}

    Right now, it looks like:

    {'Authorization': '<b64encoded value'}

    What you need to do is first generate the token_value using the Authentication endpoint.

    See this article that I wrote a while back:

    You can focus on the sections:

    Basic Authentication (Username and Password)

    Retrieving Workspace and Model Details

    You can then substitute the endpoints/URLs with the ones you want afterwards.

  • Thank you @StevenBeerthuizen and @kevin.cho.

    Really helpful. I will try the above recommendations and I will let you know.

  • Is anyone familiar with this error here? I am trying to implement the Certificate approach and it seems there is something wrong with the file packages as indicated in the error messages. If you have encounter similar problem, kindly explain how you resolved that. Thank!