Page 1 of 1

PHP cURL - REST API - get already generated AccessToken/BhRestToken - 02309332

Posted: Sun Mar 06, 2016 2:40 am
by vedant
Hello everyone,

I am creating one web application.I am using PHP cURL to use Bullhorn REST API to access their services.

The procedure is as following:

Make an API call to get Authorization code.
Using that Authorization code, make another API call to get Access Token [which is valid for 10 min].
Using that Access Token, make another Login API call to get BhRestToken(session key) and restUrl (Base url).
Using that BhRestToken and restUrl, make other API calls to access the database. [Create new records / Get some records / Update records]

Here, when I want to perform any action to access bullhorn service, I have to follow above steps. Means I am making 3-4 API calls

The problem I am facing is, I am not getting, how to check whether the previous present Access Token OR BhRestToken is valid / available so that, I can execute the last step directly. If that Access Token OR BhRestToken is not valid / available then, I will follow all the steps.

Can anyone help me to solve this issue?

Re: PHP cURL - REST API - get already generated AccessToken/BhRestToken

Posted: Tue Mar 08, 2016 2:43 pm
by DaveNorthCreek
Just save and reuse the tokens all the way up. When you get an error, go back one step in your workflow and re-apply for a new token.

Once you write the code and automate it, you just ask for a connection and don't think about it again.

My code is spread across multiple classes, but it follows that strategy. I have a test method that prints out a BhRestToken to the terminal so I can use it on Postman. I check whenever I connect to see if the previously cached token still works. If it doesn't, up the stack. No harm to get an error message from Bullhorn- they come back pretty quick.

Cheers,
Dave
North Creek

Re: PHP cURL - REST API - get already generated AccessToken/BhRestToken

Posted: Sat Mar 12, 2016 4:15 am
by vedant
Thank you for your answer. It really helped me. :)

Re: PHP cURL - REST API - get already generated AccessToken/BhRestToken

Posted: Mon Aug 01, 2016 6:33 am
by maheswari
Could you please hep me , how to generate the access token using refresh token?

Re: PHP cURL - REST API - get already generated AccessToken/BhRestToken

Posted: Mon Aug 01, 2016 1:22 pm
by ColinC
Hello maheswari,

On our getting started page (http://developer.bullhorn.com/articles/getting_started) it runs through this and more.

The section in specific is here:
Use a refresh token to get a new access token
To obtain an access token with a refresh token, make a POST request with the following URL, substituting your actual values for the placeholders in the query parameters:

https://auth.bullhornstaffing.com/oauth ... ent_secret}

I hope this helps!

Re: PHP cURL - REST API - get already generated AccessToken/BhRestToken

Posted: Mon Aug 01, 2016 1:23 pm
by ColinC
That didn't paste properly. Let's try this.

Code: Select all

https://auth.bullhornstaffing.com/oauth/token?grant_type=refresh_token&refresh_token={refresh_token}&client_id={client_id}&client_secret={client_secret}

Re: PHP cURL - REST API - get already generated AccessToken/BhRestToken

Posted: Wed Aug 03, 2016 7:43 am
by maheswari
public function BHConnect() {
try {
if (isset($_SESSION['BH']['auth_code'])) {
if ((!isset($_SESSION['BH']['restToken'])) || (isset($_SESSION['BH']['tokenCreated']) && $_SESSION['BH']['tokenCreated'] + 8 * 60 < time())) {

$token = $this->getRefreshAccess($_SESSION['BH']['refreshToken']);

$sessionToken = $this->getBHSession($token->access_token);

$this->setBHSession($sessionToken);
}
} else {
$this->setCredentials();

$authCode = $this->getBHAuthCode();

$token = $this->getBHTokens($authCode);

$sessionToken = $this->getBHSession($token->access_token);

$this->setBHSession($sessionToken);
}
} catch (Exception $e) {
return $e;
}


private function getRefreshAccess($refreshtoken) {
$method = 'token?grant_type=refresh_token&refresh_token=' . $refreshtoken . '&client_id=' .
$this->bh_client_id . '&client_secret=' . $this->bh_clinet_secret;

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

$response = $this->makeHttpRequest($this->bh_auth_url, $method, $options);

return $response;
}


How to I check the first access token was expired ?
The above code is returned error like Invalid, expired, or revoked refresh token.

Re: PHP cURL - REST API - get already generated AccessToken/BhRestToken

Posted: Wed Aug 10, 2016 6:47 am
by maryjohn
Could you please hep me , how to generate the access token using refresh token?

Re: PHP cURL - REST API - get already generated AccessToken/BhRestToken

Posted: Mon Aug 15, 2016 10:33 am
by dfuller
maryjohn wrote:Could you please hep me , how to generate the access token using refresh token?
Hi Mary,

This was covered by Colin earlier in the thread:
Plugging the refresh token, client ID, and client secret into the appropriate spots (minus the brackets) will allow you to get an access token.
How to I check the first access token was expired ?
The above code is returned error like Invalid, expired, or revoked refresh token.
Hi Maheswari,

There is not call to check and see if an access token is expired - it will just tell you once it is. My recommendation would be to re-run the auth process if the token turns out to be expired.

Warm regards,
Dan