Skip to content

Implementing REST API with python - access token request, wrong password

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

        }


 





 

Comments

  • Hello Jannes,


    currently there is a restriction on the REST API authentication which might be causing this issue.


    The Authentication only is activated correctly when you enable in a "PLCnext Engineer Projects -> HMI Webserver -> Settings -> Security -> PLCnext user management".

    AND create some (empty) page + a Login Page.

    i hope this solves this issue.

    kind regards,
    Olver.

Sign In or Register to comment.