Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.

Log in or register to post/reply in the forum.

API Call with username and password failed

Thein Nov 12, 2022 09:42 AM


I need little bit help on API call with username and password. I found this "http://username:password@http.server.address." on the help file of CR6 CR Basic programinng, so I tried I few codes as trial and error as follows, but nothing seems working. The datalogger I am using is CR6 and internet is from office ethernet connection. I have other API calls with token in the same program, all working well, except this web server address, "https://loadsensing.wocs3.com/11111/dataserver/api/v1/data/networks/11111", which need username and password.

    'Uri_3 = "https://username:password@http.loadsensing.wocs3.com/11111/dataserver/api/v1/Data/networks/11111"
    'Uri_3 = "https://username:password@https.loadsensing.wocs3.com/11111/dataserver/api/v1/Data/networks/11111"
    'Uri_3 = "https://username:password@https://loadsensing.wocs3.com/11111/dataserver/api/v1/Data/networks/11111"
    Uri_3 = "https://username:password@loadsensing.wocs3.com/11111/dataserver/api/v1/Data/networks/11111"
    HTTPGet (Uri_3,WS_Api,"",7500) 'Time out 75Sec

The python code that works for the API call is as follows:-

import json
import requests
from requests.auth import HTTPBasicAuth

uri_3 = "https://loadsensing.wocs3.com/11111/dataserver/api/v1/data/networks/11111"
response_1 = requests.get(uri_3, auth=HTTPBasicAuth("username", "password"))

aa = response_1.content
bb = json.loads(aa)
print("Tilt Readings for T1")
print("TimeStamp =", bb["22222"][3]["value"]["readTimestamp"])
print("X_Tilt =", bb["22222"][3]["value"]["readings"][0]["tilt"])
print("Y_Tilt =", bb["22222"][3]["value"]["readings"][1]["tilt"])
print("Z_Tilt =", bb["22222"][3]["value"]["readings"][2]["tilt"])

 I hope some one can enlighten me about API Call with username and password. Thanks in advace.

Best regards,

Vim Nov 14, 2022 08:48 AM

You could try using the header with the following syntax:

"Authorization: Basic [insert authentication hash]"

Example below is using admin/admin as the username and password hash 

    HTTPGet (Uri_3,WS_Api,"Authorization: Basic YWRtaW46YWRtaW4=",7500) 'Time out 75Sec
Log in or register to post/reply in the forum.