Last active
March 23, 2017 03:59
-
-
Save nguyenphuocnhatthanh/944a3acff9b438eed9ebf5a012134d04 to your computer and use it in GitHub Desktop.
Login Twitter API with Oauth1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services\Social; | |
use App\Services\Social\Contrast\TwitterServiceInterface; | |
use League\OAuth1\Client\Credentials\TokenCredentials; | |
use League\OAuth1\Client\Server\Server; | |
class TwitterService extends AbstractSocial implements TwitterServiceInterface | |
{ | |
/** | |
* The OAuth server implementation. | |
* | |
* @var \League\OAuth1\Client\Server\Server | |
*/ | |
protected $server; | |
/** | |
* Secret token | |
* | |
* @var string | |
*/ | |
private $secretToken; | |
/** | |
* Create a new provider instance. | |
* | |
* @param \League\OAuth1\Client\Server\Server $server | |
*/ | |
public function __construct(Server $server) | |
{ | |
$this->server = $server; | |
} | |
/** | |
* @param $secretToken | |
* @return $this | |
*/ | |
public function setSecretToken($secretToken) | |
{ | |
$this->secretToken = $secretToken; | |
return $this; | |
} | |
/** | |
* Get user data from Social | |
* | |
* @param $token | |
* @return array | |
*/ | |
protected function getUserByToken($token) | |
{ | |
return (array) $this->server->getUserDetails($this->getTokenCredentials($token, $this->secretToken)); | |
} | |
/** | |
* Convert data | |
* | |
* @param array $user | |
* @return array | |
*/ | |
protected function mapUserToObject(array $user) | |
{ | |
return [ | |
'id' => $user['uid'], | |
'nickname' => $user['nickname'], | |
'name' => $user['name'], | |
'email' => $user['email'] ?: strtolower(sprintf('%s@%s', $user['nickname'], 'twitter.com')), | |
'images' => [ | |
'origin' => str_replace('_normal', '', $user['imageUrl']), | |
'thumb' => $user['imageUrl'], | |
] | |
]; | |
} | |
/** | |
* @param $token | |
* @param $secret | |
* @return TokenCredentials | |
*/ | |
private function getTokenCredentials($token, $secret) | |
{ | |
$tokenCredentials = new TokenCredentials(); | |
$tokenCredentials->setIdentifier($token); | |
$tokenCredentials->setSecret($secret); | |
return $tokenCredentials; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment