Dylan Roberts
Dylan Roberts

Reputation: 11

Paylocity API Access Token

I have been trying to retreive the access token for the paylocity API. I am able to get it through postman with the client id and client secret however when I try and retrieve it with Python I get the message {"error":"invalid_client"}. This is the code that I am using

import json
import base64
import requests

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

client_id = ''

client_secret = ''

auth = (f'{client_id}:{client_secret}')



headers = {
    'content-type': "application/x-www-form-urlencoded",
        'Authorization': f"Basic {auth}"
        }

body =  "grant_type=client_credentials&scope=WebLinkAPI"

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

print (response.text)

Upvotes: 1

Views: 2451

Answers (5)

Yara1994
Yara1994

Reputation: 391

Here is the code I use

# IMPORT LIBRARIES

import urllib
import requests

# CREDENTIALS

clientId = '***'
clientSecret = '***'

prod_auth_url = 'https://api.paylocity.com/IdentityServer/connect/token'

body_params = urllib.parse.urlencode({'grant_type': 'client_credentials','scope':'WebLinkAPI'})

# AUTHORIZATION

from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import BackendApplicationClient

client = BackendApplicationClient(client_id = clientId)
oauth = OAuth2Session(client = client)
token = oauth.fetch_token(token_url = prod_auth_url, client_id = clientId, client_secret = clientSecret, body = body_params)

token

################## OR ##################


auth_response = requests.post(prod_auth_url, auth = (clientId, clientSecret), data = body_params)

response = auth_response.json()

api_call_headers = {'Authorization': 'Bearer ' + response['access_token']}

api_call_headers

Upvotes: 0

McKay Reed Moore
McKay Reed Moore

Reputation: 33

I was looking for an example in C# but couldn't find it so here is an example in C#:

    // set token URL, client ID, and client secret
    string tokenUrl = "https://apisandbox.paylocity.com/IdentityServer/connect/token";
    string clientId = "XXXX";
    string clientSecret = "XXXXXX";
    
    // set grant type and scope
    var data = new FormUrlEncodedContent(new []
    {
        new KeyValuePair<string, string>("grant_type", "client_credentials"),
        new KeyValuePair<string, string>("scope", "WebLinkAPI")
    });
    
    // create HTTP client and set authentication header
    var client = new HttpClient();
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
        "Basic", Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{clientId}:{clientSecret}"))
    );
    
    // send POST request to token URL with client credentials
    var response = await client.PostAsync(tokenUrl, data);
    
    // read response content and output to console
    var responseContent = await response.Content.ReadAsStringAsync();
    Console.WriteLine(responseContent);

Upvotes: 1

Edwin Enriquez
Edwin Enriquez

Reputation: 851

for only the token I do:

import requests, json
token_url = "https://apisandbox.paylocity.com/IdentityServer/connect/token"
#client credentials 
client_id = 'XXXX'
client_secret = 'XXXXXX'
#step A, B - single call with client credentials as the basic auth header - will return access_token
data = {'grant_type': 'client_credentials', 'scope':'WebLinkAPI'}

access_token_response = requests.post(token_url, data=data, verify=False, allow_redirects=False, auth=(client_id, client_secret))

print(access_token_response.headers)
print (access_token_response.text)

And after that code I recived the Token the same as the PostMan.

you can check: https://developer.byu.edu/docs/consume-api/use-api/oauth-20/oauth-20-python-sample-code

for more information/options.

Upvotes: 3

J. Ternent
J. Ternent

Reputation: 116

In case someone else stumbles on this response, since there are not many search hits for this:

To get the token from Paylocity and call their API:

client_id = {your client id string}
client_secret = {your client secret}
company_id = {your company id from Paylocity dashboard, without leading 'CS'}
prod_auth_url = 'https://api.paylocity.com/IdentityServer/connect/token'
body_params = urllib.parse.urlencode({'grant_type': 'client_credentials','scope':'WebLinkAPI'})
# Requests can use auth= for basic authentication
auth_response = requests.post(prod_auth_url,auth=(client_id, client_secret), data=urllib.parse.urlencode(body_params))
response = json.loads(auth_response.content)
api_call_headers = {'Authorization': 'Bearer ' + response['access_token']}
# Get all employees for a company
empl_response = requests.get(f"https://api.paylocity.com/api/v2/companies/{company_id}/employees/",headers=api_call_headers, verify=False)
pd.DataFrame(json.loads(empl_response.text))

Make sure you're using the client_id and client_secret for your token call, not the company id. It is not necessary to use any OAuth2 libraries to access the API.

Upvotes: 2

nd2
nd2

Reputation: 11

Try the following with the same variables:

from requests_oauthlib import OAuth2Session

from oauthlib.oauth2 import BackendApplicationClient

client = BackendApplicationClient(client_id=client_id)

oauth = OAuth2Session(client=client)

token = oauth.fetch_token(token_url=url, client_id=client_id, client_secret=client_secret, body=body, headers=headers)

Upvotes: 1

Related Questions