You are here

public function Salesforce::refreshToken in Salesforce Suite 7.3

Refresh access token based on the refresh token. Updates session variable.

Throws

SalesforceException

1 call to Salesforce::refreshToken()
Salesforce::apiCall in includes/salesforce.inc
Make a call to the Salesforce REST API.

File

includes/salesforce.inc, line 325
Objects, properties, and methods to communicate with the Salesforce REST API

Class

Salesforce
Ability to authorize and communicate with the Salesforce REST API.

Code

public function refreshToken() {
  $refresh_token = $this
    ->getRefreshToken();
  if (empty($refresh_token)) {
    throw new SalesforceException(t('There is no refresh token.'));
  }
  $data = drupal_http_build_query(array(
    'grant_type' => 'refresh_token',
    'refresh_token' => $refresh_token,
    'client_id' => $this->consumer_key,
    'client_secret' => $this->consumer_secret,
  ));
  $url = $this->login_url . '/services/oauth2/token';
  $headers = array(
    // This is an undocumented requirement on Salesforce's end.
    'Content-Type' => 'application/x-www-form-urlencoded',
  );
  $response = $this
    ->httpRequest($url, $data, $headers, 'POST');
  if ($response->code != 200) {

    // @TODO: Deal with error better.
    throw new SalesforceException(t('Unable to get a Salesforce access token.'), $response->code);
  }
  $data = drupal_json_decode($response->data);
  $this
    ->checkResponseDataForApiError($data);
  $this
    ->setAccessToken($data['access_token']);
  $this
    ->setIdentity($data['id']);
  $this
    ->setInstanceUrl($data['instance_url']);
}