Auth code not parsing - 02217124

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

Moderators: StaffingSupport, s.emmons, BullhornSupport

Post Reply
vinodkalpaka
User
Posts: 7
Joined: Thu Mar 03, 2016 5:56 am

Auth code not parsing - 02217124

Post by vinodkalpaka » Tue Apr 05, 2016 2:15 am

I am using PHP code to parse authorization code from the bullhorn server.
Please find my code here, which I got from google.

Code: Select all


 <?php 
//echo phpinfo();
define('USER', 'xxxxx');
define('CLIENT_ID', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
define('CLIENT_SECRET', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
define('PASS', 'xxxxxxxxx')


function getAuthCode()
{         
    $url = 'https-//auth.bullhornstaffing.com/oauth/authorize?client_id='.CLIENT_ID.'&response_type=code&action=Login&username='.USER.'&password='.PASS;
    $curl = curl_init( $url );
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, true);
//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_AUTOREFERER, true);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 120);
    curl_setopt($curl, CURLOPT_TIMEOUT, 120);

    $content = curl_exec( $curl );
    echo $content;
    curl_close( $curl );//die($content);

    if(preg_match('#Location- (.*)#', $content, $r)) {
      $l = trim($r[1]);
        $temp = preg_split("/code=/", $l);
        $authcode = $temp[1];
    }

    return $authcode;
}
function doBullhornAuth($authCode)
{
    $url = 'https-//auth.bullhornstaffing.com/oauth/token?grant_type=authorization_code&code='.$authCode.'&client_id='.CLIENT_ID.'&client_secret='.CLIENT_SECRET;

    $options = array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => array()
    );

    $ch = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );

    curl_close( $ch ); //die($content);

    return $content;

}

function doBullhornLogin($accessToken)
{
    $url = 'https-//rest.bullhornstaffing.com/rest-services/login?version=*&access_token='.$accessToken;
    $curl = curl_init( $url );
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($curl, CURLOPT_HEADER, true);
//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($curl, CURLOPT_AUTOREFERER, true);
//curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 120);
//curl_setopt($curl, CURLOPT_TIMEOUT, 120);

    $content = curl_exec( $curl );
    curl_close( $curl );
    return $content;
}

try {
    $authCode = getAuthCode();//echo $authCode;die;
    $auth = doBullhornAuth($authCode);echo $auth;die;
    $tokens = json_decode($auth);//print '<pre>';print_r($tokens);die;
   $session = doBullhornLogin($tokens->access_token);
    //echo $session;die;
} catch (Exception $e) {
    error_log($e->getMessage());
}
?>

I am getting the error
Notice: Undefined variable: authcode in
.

Please advice.

DaveNorthCreek
User
Posts: 89
Joined: Fri Nov 20, 2015 2:04 pm

Re: Auth code not parsing

Post by DaveNorthCreek » Tue Apr 05, 2016 8:25 pm

"authcode" is a variable that is being called without having been initialized. I can see that happening in "function getAuthCode()" because you only initialize $authcode inside an if statement. If there's a problem, the preg_match doesn't match, and $authcode is never set. That would cause that error.

Make sure you understand:
http://developer.bullhorn.com/articles/getting_started
http://developer.bullhorn.com/documenta ... thworkflow

and this is your best friend, once you're past that hurdle:
http://developer.bullhorn.com/sites/def ... TAPI_0.pdf

Good luck!
Dave
North Creek

vinodkalpaka
User
Posts: 7
Joined: Thu Mar 03, 2016 5:56 am

Re: Auth code not parsing

Post by vinodkalpaka » Wed Apr 06, 2016 12:53 am

The real issue is I am not getting the response from the server.

Code: Select all

$content = curl_exec( $curl );
    echo $content;
do not generate output. What may be the reasons. If I directly paste the URL in the browser, I am getting the auth code in the URL

DaveNorthCreek
User
Posts: 89
Joined: Fri Nov 20, 2015 2:04 pm

Re: Auth code not parsing

Post by DaveNorthCreek » Fri Apr 08, 2016 12:58 pm

The location field is in the header, not the body of the return. curl should not "followlocation" -> perhaps explicitly set that to false?

I parse the header out of the response and parse the code out of that.

Code: Select all

		if (preg_match("|Location: (https?://\S+)|", $headers, $m)) {
			if (preg_match("|code=(\S+)\&client_id|", $m[1], $n)) {
				$code = urldecode($n[1]);
The other issue is that you do not set a redirect_uri in your authorization request. If that is okay in your browser, it might be okay, but in my experience that always has to be there.

Hope this helps.

vinodkalpaka
User
Posts: 7
Joined: Thu Mar 03, 2016 5:56 am

Re: Auth code not parsing

Post by vinodkalpaka » Tue May 03, 2016 4:42 am

I am getting $content from the line

Code: Select all

$content = curl_exec( $curl );
How can I get the variable $headers ?

DaveNorthCreek
User
Posts: 89
Joined: Fri Nov 20, 2015 2:04 pm

Re: Auth code not parsing

Post by DaveNorthCreek » Wed May 04, 2016 1:37 pm

Here's my code - the headers are transmitted before the HTML tag:

Code: Select all

	$authResponse = $httpClient->retrieveResponse($uri2, '', [], 'GET');
	$html_start = strpos($authResponse, '<!DOCTYPE html>');
	$headers = substr($authResponse, 0, $html_start);
	$this->log_debug($headers);
	if (preg_match("|Location: (https?://\S+)|", $headers, $m)) {
		$this->log_debug("Location: ".$m[1]);
		if (preg_match("|code=(\S+)\&client_id|", $m[1], $n)) {
			$code = urldecode($n[1]);
			$servicesCredentials['bullhorn']['code'] = $code;
			$this->load_refresh_with_code($bullhornService, $httpClient, $servicesCredentials);
		}
	}

vinodkalpaka
User
Posts: 7
Joined: Thu Mar 03, 2016 5:56 am

Re: Auth code not parsing

Post by vinodkalpaka » Tue May 10, 2016 12:45 am

I am getting the error Undefined variable: httpClient .
Which library has to be included for this object ?

shossain
User
Posts: 7
Joined: Wed Dec 31, 1969 8:00 pm

Re: Auth code not parsing - 02217124

Post by shossain » Sun May 15, 2016 7:12 pm

Hello vinodkalpaka,

This is Shamim from Bullhorn Support, I see DaveNorthCreek has been assisting already. Is there anything I can help with?

Unfortunately we can't help troubleshoot the PHP code, however if you provide the REST URI we can investigate this further.

I will certainly request our documentation team to update the REST Documentation with sample PHP code, which may be a long term option.
Shamim Hossain
Senior Technical Support Analyst
B U L L H O R N

89 York Street, 11th Floor, Sydney, MA 2000
617-478-9126 (US Support)
+44 800 032 2848 ext. 9131 (UK Support)
+61 2 8073 5089 (APAC Support)

maryjohn
User
Posts: 5
Joined: Wed Aug 10, 2016 6:04 am

Re: Auth code not parsing - 02217124

Post by maryjohn » Wed Aug 10, 2016 6:49 am

"authcode" is a variable that is being called without having been initialized. I can see that happening in "function getAuthCode()" because you only initialize $authcode inside an if statement. If there's a problem, the preg_match doesn't match, and $authcode is never set. That would cause that error.
Hello I Am Mary

Post Reply