You are here

function hubspot_oauth_refresh in HubSpot 6.2

Same name and namespace in other branches
  1. 7.3 hubspot.module \hubspot_oauth_refresh()
  2. 7.2 hubspot.module \hubspot_oauth_refresh()

Refreshes Hubspot OAuth Access Token when expired.

1 call to hubspot_oauth_refresh()
hubspot_get_recent in ./hubspot.module
Gets the most recent HubSpot leads

File

./hubspot.module, line 34
Sends Webform results to HubSpot's Leads API by using Webform's provided hooks.

Code

function hubspot_oauth_refresh() {
  $data = array(
    'refresh_token' => variable_get('hubspot_refresh_token'),
    'client_id' => HUBSPOT_CLIENT_ID,
    'grant_type' => 'refresh_token',
  );
  $data = drupal_http_build_query($data);
  $options = array(
    'headers' => array(
      'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
    ),
    'method' => 'POST',
    'data' => $data,
  );
  $return = drupal_http_request('https://api.hubapi.com/auth/v1/refresh', $options);
  if ($return->code == '200') {
    $return_data = json_decode($return->data, true);
    $hubspot_access_token = $return_data['access_token'];
    variable_set('hubspot_access_token', $hubspot_access_token);
    $hubspot_refresh_token = $return_data['refresh_token'];
    variable_set('hubspot_refresh_token', $hubspot_refresh_token);
    $hubspot_expires_in = $return_data['expires_in'];
    variable_set('hubspot_expires_in', $hubspot_expires_in);
    return TRUE;
  }
  else {
    drupal_set_message(t('Refresh token failed with Error Code "%code: %status_message". Reconnect to your Hubspot account.'), 'error', FALSE);
    watchdog('hubspot', 'Refresh token failed with Error Code "%code: %status_message". Visit the Hubspot module settings page and reconnect to your Hubspot account.', array(
      '%code' => $return->code,
      '%status_message' => $return->status_message,
    ), WATCHDOG_INFO);
    return FALSE;
  }
}