You are here

function hubspot_oauth_refresh in HubSpot 7.3

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

Refreshes HubSpot OAuth Access Token when expired.

2 calls to hubspot_oauth_refresh()
hubspot_get_recent in ./hubspot.module
Gets the most recent HubSpot leads.
_hubspot_webform_get_forms in hubspot_webform/hubspot_webform.admin.inc
Gets the list of forms from HubSpot via the API.

File

./hubspot.module, line 39
Sends Webform results to HubSpot's Forms API.

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;
  }
}