Help working with Bullhorn API

Forum for users and developers of Bullhorn's API service.

Moderators: StaffingSupport, s.emmons, BullhornSupport

Post Reply
pawel12123
User
Posts: 5
Joined: Tue Jul 02, 2019 4:15 pm

Help working with Bullhorn API

Post by pawel12123 » Tue Jul 16, 2019 2:15 pm

Hey Guys, So my ultimate goal is to be able to merge/delete all duplicate data in our bullhorn database. However, I just recently started working with API's. I'm using python and managed to get the authentication process done. and I'm able to send a get request but when I try to send a put or post request I struggle.

Code: Select all

import requests
import urllib.parse
import json

from datetime import datetime, timedelta

#Vars and Stuff--------------------------------------------------------------------------------------------------------------------------------------
AUTH_URL = "https://auth.bullhornstaffing.com/oauth"
REST_URL = "https://rest.bullhornstaffing.com/rest-services"

auth = dict(
    client = 'XXXXXXXXXXX',
    secret = 'XXXXXXXXXXXX',
    username = "XXXXXXXXXX",
    password = "XXXXXXXX")

#BHAuth Stuff----------------------------------------------------------------------------------------------------------------------------------------

def get_auth_code():
    params = dict(
            client_id= auth['client'],
            response_type= "code",
            username= auth['username'],
            password= auth['password'],
            action= "Login")

    url = AUTH_URL + "/authorize"
    req = requests.get(url, params=params)
    response_url = req.url
    response_query = requests.utils.urlparse(response_url).query
    #Parses return URL to get us just the code.
    query_dict = urllib.parse.parse_qs(response_query)
    if query_dict.get("code"):
        auth_code = query_dict["code"][0]
    else:
        auth_code = ""
    return auth_code


def get_access_token(auth_code):
    params = dict(
            client_id=auth['client'],
            client_secret=auth['secret'],
            code = auth_code,
            grant_type="authorization_code")
    
    url = AUTH_URL + "/token"

    req = requests.post(url, params=params)
    access_token_url = req.url
    try:
        response_dict = req.json()
    except Exception as err:
        print(err)
        response_dict = {}
    return(response_dict)
#BHRest Stuff----------------------------------------------------------------------------------------------------------------------------------------

version = "*"

import pprint
def rest_login(access_token):
    url = REST_URL +"/login"
    login_params = dict(
            version = version,
            access_token = access_token['access_token'])
    req = requests.get(url, params=login_params)
    try:
        response_dict = req.json()
    except Exception as err:
        print(err)
        rest_login = {}
    rest_login = response_dict
    return rest_login

def find(url, rest_token, query="Unknown", meta="full", showEditable="true", countPerEntity="500"):
        """ Searches the following entity types given a string containing search terms:
            - ClientContact
            - JobOrder
            - Candidate
            - ClientCorporation
            - Lead (if leadsAndOpportunitiesEnabled = true)
            - Opportunity (if leadsAndOpportunitiesEnabled = true)
            To search through the phone numbers start query with the "+" and append last 8 digits.
        """
        url = url + "/find"
        params = dict(
            BhRestToken=rest_token,
            query=query,
            meta=meta,
            showEditable=showEditable,)
        req = requests.get(url, params=params)
        try:
            response_dict = req.json()
            #pprint.pprint(response_dict)
            print(response_dict['data'][1]['entityId'])
        except Exception as err:
            print(err)
            response_dict = {} 
        return response_dict.get("data")

#https://rest.bullhornstaffing.com/rest-services/{corpToken}/entity/Candidate/{id}?BhRestToken={session_key}&fields=firstName,lastName,address

def put(url1, rest_token):
    url = url1 + 'entity/Candidate'
    params = dict(BhRestToken=rest_token)
    data = {'firstName' : 'TEST','lastName':'TEST'}
    
    print(requests.put(url, params = params, data = data))
    
def post(url1, rest_token):
    url = url1 + "entity/Candidate/30707"
    data = {"title":"TEST NAME"}
    req = requests.post(url, data = data, params = dict(BhRestToken = rest_token))
    try:
        response_dict = req.json()
    except Exception as err:
        print(err)
        response_dict = {}
    print(response_dict)
#Main------------------------------------------------------------------------------------------------------------------------------------------------
def main():
    auth_code = get_auth_code()
    access_tokenDict = get_access_token(auth_code)
    BhRestTokenDict = rest_login(access_tokenDict)
    #print(access_tokenDict)
    #print(BhRestTokenDict)
    foundDict = find(BhRestTokenDict['restUrl'], BhRestTokenDict['BhRestToken'])
    #pprint.pprint(foundDict)
    #print(BhRestTokenDict['restUrl'])
    print(put(BhRestTokenDict['restUrl'], BhRestTokenDict['BhRestToken']))

Can someone help me with post and put requests? FYI a lot of the code above was found in an online python script.

kschutten
User
Posts: 17
Joined: Tue Jun 25, 2019 2:22 pm

Re: Help working with Bullhorn API

Post by kschutten » Thu Jul 18, 2019 3:39 pm

Hi Pawel 12123,

If your question is to do your work for you, I guess the answer is that no one here on this forum will do that....
You will need to rewrite the script yourself, but maybe we can help you to find the cause or get you on your way;

What are the exact errors you get?
Have you tried the same with Postman (https://www.getpostman.com/) or another API client / tool? (Same errors?)

Post Reply