0
votes

I'm trying to generate an access token from an API using the following required information:

Authorization endpoint: https://api.paylocity.com/IdentityServer/connect/token

Authorization Header The request is expected to be in the form of a basic authentication request, with the "Authorization" header containing the client-id and client-secret. This means the standard base-64 encoded user:password, prefixed with "Basic" as the value for the Authorization header, where user is the client-id and password is the client-secret.

Content-Type Header The "Content-Type" header is required to be "application/x-www-form-urlencoded".

Other Values The request must post the following form encoded values within the request body:

grant_type = client_credentials scope = WebLinkAPI

I've tried using the Python 'requests' package without success, see below:

import requests

url = "https://api.paylocity.com/IdentityServer/connect/token"

headers = {
    'Authorization': "Basic **********:**********",
    'Content-Type': "application/x-www-form/urlencoded"
    }

body = {
    'grant_type': "client_credentials",
    'scope': 'WebLinkAPI'
}

response = requests.request("POST", url, headers=headers, params=body)

print(response.text)

Instead of an access token, I receive an error message:

{"error":"invalid_client"}

I've verified that my base64 encoded username and password are correct, it looks something like "G/RaNdOm:MoReRaNdOm==" and when I add them to postman it works so my credentials are correct. What could be wrong?

1

1 Answers

1
votes

I resolved this. There were two issues, the Authorization header was not using ASCII as the destination charset with base64 encoding. The other issue was trying to add the additional values to the parameters instead of the body in the requests package. The solution was to replace "params" with "data". Here was the solution:

url = "https://api.paylocity.com/IdentityServer/connect/token"

body = "grant_type=client_credentials&scope=WebLinkAPI"
headers = {
    'Content-Type': "application/x-www-form-urlencoded",
    'Authorization': "Basic ***(base64ASCII)username:password***",
    }

response = requests.request("POST", url, data=body, headers=headers)

print(response.text)