You are here

public function SalesforceController::oauthCallback in Salesforce Suite 8.3

OAuth step 2: Callback for the oauth redirect URI.

Complete OAuth handshake by exchanging an authorization code for an access token.

1 string reference to 'SalesforceController::oauthCallback'
salesforce.routing.yml in ./salesforce.routing.yml
salesforce.routing.yml

File

src/Controller/SalesforceController.php, line 66

Class

SalesforceController
OAuth callback handler.

Namespace

Drupal\salesforce\Controller

Code

public function oauthCallback() {

  // If no code is provided, return access denied.
  if (empty($this
    ->request()
    ->get('code'))) {
    throw new AccessDeniedHttpException();
  }
  $data = urldecode(UrlHelper::buildQuery([
    'code' => $this
      ->request()
      ->get('code'),
    'grant_type' => 'authorization_code',
    'client_id' => $this->client
      ->getConsumerKey(),
    'client_secret' => $this->client
      ->getConsumerSecret(),
    'redirect_uri' => $this->client
      ->getAuthCallbackUrl(),
  ]));
  $url = $this->client
    ->getAuthTokenUrl();
  $headers = [
    // This is an undocumented requirement on SF's end.
    'Content-Type' => 'application/x-www-form-urlencoded',
  ];
  $response = $this->httpClient
    ->post($url, [
    'headers' => $headers,
    'body' => $data,
  ]);
  $this->client
    ->handleAuthResponse($response);
  $this
    ->successMessage();
  return new RedirectResponse($this->url_generator
    ->generateFromRoute('salesforce.authorize', [], [
    "absolute" => TRUE,
  ], FALSE));
}