# Unable to get a token when accessing via Python 2.7 requests module

**URL:** <https://forum.ansible.com/t/unable-to-get-a-token-when-accessing-via-python-2-7-requests-module/26299>\
**Category:** AWX Project\
**Created:** [December 15, 2017, 1:11am UTC](https://forum.ansible.com/t/unable-to-get-a-token-when-accessing-via-python-2-7-requests-module/26299 "2017-12-15T01:11:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![callmeshawn](https://avatars.discourse-cdn.com/v4/letter/c/cab0a1/32.png) [@callmeshawn](https://forum.ansible.com/u/callmeshawn)\
**Post date:** [December 15, 2017, 1:11am UTC](https://forum.ansible.com/t/unable-to-get-a-token-when-accessing-via-python-2-7-requests-module/26299/1 "2017-12-15T01:11:27Z")

</div>

Hello,

After beating my head on this and quite honestly confusing myself pretty well, I’m putting this out to the list for some help.

I am able to use curl to:

1. Get a token
2. Use the token acquired in step 1 and issue another curl command to delete a host from inventory

I’m trying to do the same thing using a playbook I’ve written, and a Python program, specifically the requests module.  
If all else fails, I can revert to using a shell script, but using Python would be my preferred route.

I’m just not sure how to structure my call.

This is my playbook:

---

<div class="post-metadata">

**Author:** ![rpetrell](https://avatars.discourse-cdn.com/v4/letter/r/b4bc9f/32.png) [@rpetrell](https://forum.ansible.com/u/rpetrell)\
**Post date:** [December 15, 2017, 3:32am UTC](https://forum.ansible.com/t/unable-to-get-a-token-when-accessing-via-python-2-7-requests-module/26299/2 "2017-12-15T03:32:04Z")

</div>

Hey Shawn,

You’re close 🙂 - I think your example code is sending application/x-www-form-urlencoded

Try this:

r = requests.post(ANSIBLE\_URI, json={‘username’: ANSIBLE\_USER, ‘password’: ANSIBLE\_PASS}, verify=False)

---

<div class="post-metadata">

**Author:** ![callmeshawn](https://avatars.discourse-cdn.com/v4/letter/c/cab0a1/32.png) [@callmeshawn](https://forum.ansible.com/u/callmeshawn)\
**Post date:** [December 15, 2017, 4:11am UTC](https://forum.ansible.com/t/unable-to-get-a-token-when-accessing-via-python-2-7-requests-module/26299/3 "2017-12-15T04:11:46Z")

</div>

Ryan,

That was it!  
I’m able to get a token.

“stdout”: “{"token": "f750f5f337adbe87a3d4c20e7cfa6fea4f96bbc9", "expires": "2017-12-15T04:40:39.499Z"}”,  
“stdout\_lines”: [  
“{"token": "f750f5f337adbe87a3d4c20e7cfa6fea4f96bbc9", "expires": "2017-12-15T04:40:39.499Z"}”  
]

Thank you for your help!
