Python API Request Response Returns a 401 Unathorized but Works in Curl

I’m running into a 401 error when trying to post data. However, the same data entered in a curl request works just fine. I have tried multiple attempts at trying get a 201 or 200 response.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import requests
import arubaapi
import json
import getpass
import urllib3
from urllib3.exceptions import InsecureRequestWarning
urllib3.disable_warnings()
dummyUsernam = input("Please enter your username: ")
print("n")
dummyPassword = getpass.getpass("please enter your password: ")
print("n")
aosDeviceFqdn = 'sjc04-wwi-mm-node1.mine.com'
def aosLogin():
#dummyUsernam = input("Please enter your username: ")
#print("n")
#dummyPassword = getpass.getpass("please enter your password: ")
#print("n")
"""Logs into the AOS device, returns <class requests.Session()> object and UID string"""
session = requests.Session()
myUrl = f'https://{aosDeviceFqdn}:4343/v1/api/login'
credentials = {
'username': dummyUsernam,
'password': dummyPassword,
}
#try:
#loginResponse = session.get( url=myUrl, params=credentials, verify=False )
loginResponse = session.post( url=myUrl, data=credentials, verify=False ) # same result
#except requests.exceptions.ConnectionError:
#print(f"Failed to reach {aosDeviceFqdn}, check that you are connected to VPN and that {aosDeviceFqdn} resolves properly in DNS")
#exit()
#except KeyError:
#print ("Please check the username and password, and that your account has permission to log in and is not locked.")
#exit()
jsonLoginResponse = loginResponse.json()
arubaUid1 = jsonLoginResponse['_global_result'] # this method of retrieving the UID is only available in the response of the login action
#arubaUid2 = getUidFromSession(session) # this is the same result as arubaUid1, extracted a different way, available anytime an open, logged-in session exists
global sub
sub = arubaUid1.get("UIDARUBA")
print(sub)
return sub
def func_2():
myUrl1 = ('https://10.183.73.15:4343/v1/configuration/object/airmatch_ap_freeze?config_path=%2Fmd%2FTesla-Workplace%2FSJC51')
myHeaders1 = {'Content-Type': 'application/json','Accept': 'application/json'}
myParams1 = {'UIDARUBA': sub}
# shut
myData1 = r'{ "ap-name":"sjc51-api-5m-10" , "channel":"104-" , "band_info":"5GHz" }'
response = requests.post(url=myUrl1, headers=myHeaders1, params=myParams1, data=myData1, verify=False)
print(response)
#return sub
aosLogin()
func_2()
############End of Code#########
output:
Desktop % python3 start_over_api_call.py
Please enter your username: myusername
please enter your password:
NzI1MDBmMmItYjg0Mi00MGJjLTkwODctZGU0
<Response [401]>
</code>
<code>import requests import arubaapi import json import getpass import urllib3 from urllib3.exceptions import InsecureRequestWarning urllib3.disable_warnings() dummyUsernam = input("Please enter your username: ") print("n") dummyPassword = getpass.getpass("please enter your password: ") print("n") aosDeviceFqdn = 'sjc04-wwi-mm-node1.mine.com' def aosLogin(): #dummyUsernam = input("Please enter your username: ") #print("n") #dummyPassword = getpass.getpass("please enter your password: ") #print("n") """Logs into the AOS device, returns <class requests.Session()> object and UID string""" session = requests.Session() myUrl = f'https://{aosDeviceFqdn}:4343/v1/api/login' credentials = { 'username': dummyUsernam, 'password': dummyPassword, } #try: #loginResponse = session.get( url=myUrl, params=credentials, verify=False ) loginResponse = session.post( url=myUrl, data=credentials, verify=False ) # same result #except requests.exceptions.ConnectionError: #print(f"Failed to reach {aosDeviceFqdn}, check that you are connected to VPN and that {aosDeviceFqdn} resolves properly in DNS") #exit() #except KeyError: #print ("Please check the username and password, and that your account has permission to log in and is not locked.") #exit() jsonLoginResponse = loginResponse.json() arubaUid1 = jsonLoginResponse['_global_result'] # this method of retrieving the UID is only available in the response of the login action #arubaUid2 = getUidFromSession(session) # this is the same result as arubaUid1, extracted a different way, available anytime an open, logged-in session exists global sub sub = arubaUid1.get("UIDARUBA") print(sub) return sub def func_2(): myUrl1 = ('https://10.183.73.15:4343/v1/configuration/object/airmatch_ap_freeze?config_path=%2Fmd%2FTesla-Workplace%2FSJC51') myHeaders1 = {'Content-Type': 'application/json','Accept': 'application/json'} myParams1 = {'UIDARUBA': sub} # shut myData1 = r'{ "ap-name":"sjc51-api-5m-10" , "channel":"104-" , "band_info":"5GHz" }' response = requests.post(url=myUrl1, headers=myHeaders1, params=myParams1, data=myData1, verify=False) print(response) #return sub aosLogin() func_2() ############End of Code######### output: Desktop % python3 start_over_api_call.py Please enter your username: myusername please enter your password: NzI1MDBmMmItYjg0Mi00MGJjLTkwODctZGU0 <Response [401]> </code>
import requests
import arubaapi
import json
import getpass
import urllib3
from urllib3.exceptions import InsecureRequestWarning

urllib3.disable_warnings()

dummyUsernam = input("Please enter your username: ")
print("n")
dummyPassword = getpass.getpass("please enter your password: ")
print("n") 


aosDeviceFqdn = 'sjc04-wwi-mm-node1.mine.com'

def aosLogin():

    #dummyUsernam = input("Please enter your username: ")
    #print("n")
    #dummyPassword = getpass.getpass("please enter your password: ")
    #print("n")

    """Logs into the AOS device, returns <class requests.Session()> object and UID string"""

    session = requests.Session()

    myUrl = f'https://{aosDeviceFqdn}:4343/v1/api/login'
    credentials = {
        'username': dummyUsernam,
        'password': dummyPassword,
    }

#try:
#loginResponse = session.get( url=myUrl, params=credentials, verify=False )
    loginResponse = session.post( url=myUrl, data=credentials, verify=False ) # same result

#except requests.exceptions.ConnectionError: 
    #print(f"Failed to reach {aosDeviceFqdn}, check that you are connected to VPN and that {aosDeviceFqdn} resolves properly in DNS")
    #exit()

#except KeyError:
    #print ("Please check the username and password, and that your account has permission to log in and is not locked.")
    #exit()

    jsonLoginResponse = loginResponse.json()
    arubaUid1 = jsonLoginResponse['_global_result'] # this method of retrieving the UID is only available in the response of the login action
#arubaUid2 = getUidFromSession(session) # this is the same result as arubaUid1, extracted a different way, available anytime an open, logged-in session exists
    global sub
    sub = arubaUid1.get("UIDARUBA")

    print(sub)
    return sub

def func_2():
    myUrl1 = ('https://10.183.73.15:4343/v1/configuration/object/airmatch_ap_freeze?config_path=%2Fmd%2FTesla-Workplace%2FSJC51')
    myHeaders1 = {'Content-Type': 'application/json','Accept': 'application/json'}
    myParams1 = {'UIDARUBA': sub}

# shut
    myData1 = r'{ "ap-name":"sjc51-api-5m-10" , "channel":"104-" ,  "band_info":"5GHz" }'
    response = requests.post(url=myUrl1, headers=myHeaders1, params=myParams1,  data=myData1, verify=False)
    print(response)
#return sub

aosLogin()
func_2()
############End of Code#########
output:
Desktop % python3 start_over_api_call.py
Please enter your username: myusername


please enter your password: 


NzI1MDBmMmItYjg0Mi00MGJjLTkwODctZGU0
<Response [401]>

Can anyone help identify what the issue could be? Ideally my account and creds are authorized as it works in Curl. I feel like the session is closing and the token is being rendered as expired.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật