'code', 'client_id' => $this->appID, 'redirect_uri'=> $this->callback, 'scope'=> \implode(' ', $this->getScopes()), 'state' => \json_encode($this->state) ]); } /** * @param string $code * * @return array */ public function getTokens(string $code): array { $headers = [ "Authorization: Basic " . \base64_encode($this->appID . ":" . $this->appSecret), "Content-Type: application/x-www-form-urlencoded", ]; $result = $this->request( 'POST', 'https://login.salesforce.com/services/oauth2/token', $headers, \http_build_query([ 'code' => $code, 'redirect_uri' => $this->callback , 'grant_type' => 'authorization_code' ]) ); $result = \json_decode($result, true); return [ 'access' => $result['access_token'], 'refresh' => $result['refresh_token'] ]; } /** * @param string $accessToken * * @return string */ public function getUserID(string $accessToken): string { $user = $this->getUser($accessToken); if (isset($user['user_id'])) { return $user['user_id']; } return ''; } /** * @param string $accessToken * * @return string */ public function getUserEmail(string $accessToken): string { $user = $this->getUser($accessToken); if (isset($user['email'])) { return $user['email']; } return ''; } /** * @param string $accessToken * * @return string */ public function getUserName(string $accessToken): string { $user = $this->getUser($accessToken); if (isset($user['name'])) { return $user['name']; } return ''; } /** * @param string $accessToken * * @return array */ protected function getUser(string $accessToken): array { if (empty($this->user)) { $user = $this->request('GET', 'https://login.salesforce.com/services/oauth2/userinfo?access_token='.\urlencode($accessToken)); $this->user = \json_decode($user, true); } return $this->user; } }