/** * Retrieves an access token for the given authorization code * (previously generated from www.github.com on behalf of a specific user). * The authorization code is sent to api.github.com/oauth * and a legitimate access token is generated provided the access token * and the user for which it was generated all match, and the user is * either logged in to Github or has granted an offline access permission. * * @param string $code An authorization code. * @param null $redirectUri * @return mixed An access token exchanged for the authorization code, or false if an access token could not be generated. */ protected function getAccessTokenFromCode($code, $redirectUri = NULL) { if (empty($code)) { return FALSE; } if ($redirectUri === NULL) { $redirectUri = $this->getCurrentUrl(); parse_str($redirectUri->getQuery(), $query); unset($query['code'], $query['state']); $redirectUri->setQuery($query); } try { $url = $this->config->createUrl('oauth', 'access_token', array('client_id' => $this->config->appId, 'client_secret' => $this->config->appSecret, 'code' => $code, 'redirect_uri' => $redirectUri)); $response = $this->httpClient->makeRequest(new Api\Request($url, Api\Request::POST, array(), array('Accept' => 'application/json'))); if (!$response->isOk() || !$response->isJson()) { return FALSE; } $token = $response->toArray(); } catch (\Exception $e) { // most likely that user very recently revoked authorization. // In any event, we don't have an access token, so say so. return FALSE; } return isset($token['access_token']) ? $token['access_token'] : FALSE; }
/** * @return string */ public function getUrl() { return (string) $this->config->createUrl('oauth', 'authorize', $this->getQueryParams()); }