Requests a token for a given code

@param $code

@return mixed
@throws \Exception
495332
public function requestToken($code)
    {
        $parameters = [
            'client_id' => config('twitch-api.client_id'),
            'client_secret' => config('twitch-api.client_secret'),
            'redirect_uri' => config('twitch-api.redirect_url'),
            'code' => $code,
            'grant_type' => 'authorization_code'
        ];

        try {
            $client = new Client();

            $response = $client->post(config('twitch-api.api_url') . '/kraken/oauth2/token', ['body' => $parameters]);

            $response = $response->json();

            if (isset($response[ 'access_token' ])) {

                return $response[ 'access_token' ];
            }

        } catch (\Exception $e) {

            throw $e;
        }
    }
409778
public function requestAccessToken($code)
    {
        $client = $this->getClient();

        try {
            $response = $client->request(
                'POST',
                '/token',
                [
                    'auth' => [
                        $this->clientId,
                        $this->clientSecret
                    ],
                    'form_params' => [
                        'grant_type'    => 'authorization_code',
                        'code'          => $code,
                        'client_id'     => $this->clientId,
                        'client_secret' => $this->clientSecret
                    ]
                ]
            );
        } catch (ClientException $ex) {
            $result = $this->getDecodedBody($ex->getResponse()->getBody());

            if (is_array($result) && isset($result['error'])) {
                // handle a service error message
                $message = 'Service responsed with error code "' . $result['error'] . '".';

                if (isset($result['error_description']) && $result['error_description']) {
                    $message .= ' Description "' . $result['error_description'] . '".';
                }
                throw new AuthRequestException($message, 0, $ex);
            }

            // unknown error. not parsed error
            throw $ex;
        }

        try {
            $result = $this->getDecodedBody($response->getBody());
        } catch (\RuntimeException $ex) {
            throw new AuthResponseException('Server response can\'t be parsed', 0, $ex);
        }

        if (!is_array($result)) {
            throw new AuthResponseException('Server response has unknown format');
        }

        if (!isset($result['access_token'])) {
            throw new AuthResponseException('Server response doesn\'t contain access token');
        }

        $this->setAccessToken($result['access_token']);

        $lifetimeInSeconds = $result['expires_in'];

        $expireDateTime = new \DateTime();
        $expireDateTime->add(new \DateInterval('PT'.$lifetimeInSeconds.'S'));

        $this->setExpiresIn($expireDateTime);

        return $this;
    }
698655
public function getAccessToken($code = '')
    {
        if (!empty($code)) {
            $uri = $this->buildUrl('accessToken', []);
            $guzzle = new GuzzleClient([
                'headers' => [
                    'Content-Type' => 'application/json',
                    'x-li-format' => 'json',
                    'Connection' => 'Keep-Alive'
                ]
            ]);
            try {
                $response = $guzzle->post($uri, ['form_params' => [
                    'grant_type' => self::OAUTH2_GRANT_TYPE,
                    self::OAUTH2_RESPONSE_TYPE => $code,
                    'redirect_uri' => $this->getRedirectUrl(),
                    'client_id' => $this->getClientId(),
                    'client_secret' => $this->getClientSecret(),
                ]]);
            } catch (RequestException $exception) {
                throw Exception::fromRequestException($exception);
            }
            $this->setAccessToken(
                AccessToken::fromResponse($response)
            );
        }
        return $this->accessToken;
    }
565829
private function response($code)
    {
        switch ($code) {
            // Successful
            case 200:
            case 304:
                return $this->success();
            // Non existent user
            case 403: // Forbidden:
                return $this->forbidden();
            case 404: // Removed
                return $this->requestDoesNotExist();
            // Rate limit
            case 429:
                return $this->rateLimit();
            // OAuth Credentials are NOT VALID or others
            case 400:
            default:
                return $this->generalError();
        }
    }
802861
public function get($code = null): string
    {
        if (!$this->validate($code)) {
            throw new \InvalidArgumentException('Invalid code');
        }

        return isset(self::$codes[$code]) ? self::$codes[$code] : '';
    }
1154434
public function authenticate($code)
    {
        if (empty($code)) {
            throw new NostoException('Invalid authentication token');
        }

        $request = new HttpRequest();
        $request->setUrl(Nosto::getOAuthBaseUrl() . self::PATH_TOKEN);
        $request->setReplaceParams(
            array(
                '{cid}' => $this->clientId,
                '{sec}' => $this->clientSecret,
                '{uri}' => $this->redirectUrl,
                '{cod}' => $code
            )
        );
        $response = $request->get();
        $result = $response->getJsonResult(true);

        if ($response->getCode() !== 200) {
            throw ExceptionBuilder::fromHttpRequestAndResponse($request, $response);
        }
        if (empty($result['access_token'])) {
            throw new NostoException('No "access_token" returned after authenticating with code');
        }
        if (empty($result['merchant_name'])) {
            throw new NostoException('No "merchant_name" returned after authenticating with code');
        }

        return NostoOAuthToken::create($result);
    }
373111
public function getAuth($code)
    {
        $result = $this->_curl(
            'https://api.weixin.qq.com/sns/oauth2/access_token',
            [
                'appid' => $this->getConfig('appid'),
                'secret' => $this->getConfig('secret'),
                'code' => $code,
                'grant_type' => 'authorization_code',
            ]
        );

        if ($result) {
            return [
                'access_token' => $result['access_token'],
                'openid' => $result['openid'],
            ];
        }

        return false;
    }
1080536
public static function getMessageFromCode($code)
    {
        if (!array_key_exists($code, self::$responseCodes)) {
            throw new \Exception('The header code '.$code.' is not valid.');
        }

        return self::$responseCodes[$code];
    }
418860
private function acquireToken($code)
    {
        // Request the token
        $client = new Client(); //GuzzleHttp\Client

        try {
            $response = $client->post($this->token_endpoint, [
                'form_params' => $this->getAccessTokenFields($code),
                'curl' => $this->setOauthCurlopts(),
                'synchronous' => true,
            ]);

            // Get the token and type from the response
            return json_decode($response->getBody(), true);

        } catch (\GuzzleException $e) {

            return false;
        }
    }
270471
public function getAccessToken($code = '')
	{
		$dta = array('client_id' => $this->_API['API_KEY'], 'client_secret' => $this->_API['API_SECRET'], 'code' => $code);
		$data = $this->call(['METHOD' => 'POST', 'URL' => 'https://' . $this->_API['SHOP_DOMAIN'] . '/admin/oauth/access_token', 'DATA' => $dta], FALSE);

		return $data->access_token;
	}
535897
public function code ( $code = NULL ) {
      if ( $code === NULL )
        return $this->code;
      if ( ResponseCode::find( $code ) === NULL )
        throw new \InvalidArgumentException( "Response: no code $code" );
      $this->code = intval( $code );
      return $this;
    }
1343239
public static function get($code)
    {
        try {
            return static::getByAlpha2($code);
        } catch (OutOfBoundsException $e) {
            try {
                return static::getByAlpha3($code);
            } catch (OutOfBoundsException $e) {
                try {
                    return static::getByNumeric3($code);
                } catch (OutOfBoundsException $e) {
                    throw new OutOfBoundsException("The code \"{$code}\" is not listed in ISO 4217");
                }
            }
        }
    }
912034
public function get($code = null)
    {
        try {
            return $this->findFileObject($code)->output('inline', true);
        }
        catch (Exception $ex) {}

        return Response::make(View::make('backend::404'), 404);
    }
131021
public function getAuthorizationCode($code)
    {
        $sql  = sprintf('SELECT * from %s where authorization_code = :code', $this->config['code_table']);
        $code = $this->fetch($sql, compact('code'));
        if ($code) {
            // convert date string back to timestamp
            $code['expires'] = strtotime($code['expires']);
        }

        return $code;
    }
1245582
private function requestAccess($code)
    {
        $this->imgur->requestAccessToken($code);

        // save the new token
        $token = $this->imgur->getAccessToken();
        $this->store->set('imgur-token', $token);

        // authenticate the user with the new token
        $this->authenticateUser($token);
    }
304090
public function getAccessTokenResponse(string $code)
    {
        $response = $this->getHttpClient()->post($this->getTokenUrl(), [
            'headers' => ['Accept' => 'application/json'],
            'form_params' => $this->getTokenFields($code),
        ]);
        return json_decode($response->getBody(), true);
    }
788380
public static function getLanguageForCode($code)
    {
        $language = KlarnaLanguage::fromCode($code);

        if ($language === null) {
            throw new Klarna_UnknownLanguageException($code);
        }
        return $language;
    }