Hi, I’m trying to write an application in python3 where one part of the application is an interface to the plc runtime. I managed to do it with an opcua client before, but now I’d like to use the REST API. The controller is HW02 and upgraded to FW2020. Currently I fail to get access to the plc. The code below is an authorisation request followed by an access token request. The response to the authorisation request will be ok[200] but the response to the accesstokenrequest will always be [401] with reason ‘wrong password’ I just use the admin user with the default printed password.
def RequestAuthorisation(url): uri = ‘/_pxc_api/auth/auth-token’ url = ‘{0}{1}’.format(url, uri) mySecret = ‘myappsecret’ payload = { ‘response_type’: ‘code’, ‘state’: mySecret, ‘scope’: ‘variables’ } r = requests.post(url=url, data=json.dumps(payload), verify=False) if r: return { ‘ok’: True, ‘code’: r.json()[‘code’] } else: return { ‘ok’: False }
def RequestAccess(url, authorisationCode): uri = ‘/_pxc_api/auth/access-token’ url = ‘{0}{1}’.format(url, uri) payload = { ‘code’: authorisationCode, ‘grant_type’: ‘authorization_code’, ‘username’: constant.USER, ‘password’: constant.PASSWORD } r = requests.post(url=url, data=json.dumps(payload), verify=False) if r: return { ‘ok’: True, ‘access_token’: r.json()[‘access_token’] } else: return { ‘ok’: False } url = 'https://{0}'>.format(constant.IP) authorisation = RequestAuthorisation(url) if authorisation[‘ok’]: RequestAccess(url, authorisation[‘code’])