/** * {@inheritdoc} */ public function getUser() : User { $oauthToken = self::request()->getQuery('oauth_token'); $oauthVerifier = self::request()->getQuery('oauth_verifier'); if (!$oauthToken || !$oauthVerifier) { throw new \LogicException('No code found on oauth route end'); } $token = $this->service->getStorage()->retrieveAccessToken('Twitter'); // This was a callback request from twitter, get the token $this->service->requestAccessToken($oauthToken, $oauthVerifier, $token->getRequestTokenSecret()); // Send a request now that we have access token $result = json_decode($this->service->request('account/verify_credentials.json?include_email=true'), true); $name = $this->pop($result, 'name'); $firstname = $lastname = null; if ($name) { $explode = explode(' ', $name); if (isset($explode[0])) { $firstname = array_shift($explode); $lastname = implode(' ', $explode); } } $user = new User($this->getType()); $user->setUid($this->pop($result, 'id_str'))->setEmail($this->pop($result, 'email'))->setUsername($this->pop($result, 'screen_name'))->setVerified($this->pop($result, 'verified'))->setFirstName($firstname)->setLastName($lastname)->setLocale($this->pop($result, 'lang'))->setExtraData($result); return $user; }
/** * @covers OAuth\OAuth1\Service\Twitter::__construct * @covers OAuth\OAuth1\Service\Twitter::getRequestTokenEndpoint * @covers OAuth\OAuth1\Service\Twitter::parseAccessTokenResponse */ public function testParseAccessTokenResponseValid() { $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('oauth_token=foo&oauth_token_secret=bar')); $token = $this->getMock('\\OAuth\\OAuth1\\Token\\TokenInterface'); $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); $storage->expects($this->any())->method('retrieveAccessToken')->will($this->returnValue($token)); $service = new Twitter($this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), $client, $storage, $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')); $this->assertInstanceOf('\\OAuth\\OAuth1\\Token\\StdOAuth1Token', $service->requestAccessToken('foo', 'bar', $token)); }