Skip to content

Commit

Permalink
Fixes #139: fix error about twitter scope when getting temporary cred…
Browse files Browse the repository at this point in the history
…entials
  • Loading branch information
Diego Sinclair authored and bencorlett committed Aug 15, 2021
1 parent 20dd381 commit 0a423aa
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ $server = new League\OAuth1\Client\Server\Twitter([
'identifier' => 'your-identifier',
'secret' => 'your-secret',
'callback_uri' => "http://your-callback-uri/",
'scope' => 'your-application-scope' // optional ('read', 'write'), empty by default
]);
```

Expand Down
84 changes: 83 additions & 1 deletion src/Server/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,58 @@

class Twitter extends Server
{
/**
* Application scope.
*
* @var ?string
*/
protected $applicationScope = null;

/**
* @inheritDoc
*/
public function __construct($clientCredentials, SignatureInterface $signature = null)
{
parent::__construct($clientCredentials, $signature);

if (is_array($clientCredentials)) {
$this->parseConfiguration($clientCredentials);
}
}

/**
* Set the application scope.
*
* @param ?string $applicationScope
*
* @return Twitter
*/
public function setApplicationScope($applicationScope)
{
$this->applicationScope = $applicationScope;

return $this;
}

/**
* Get application scope.
*
* @return ?string
*/
public function getApplicationScope()
{
return $this->applicationScope;
}

/**
* @inheritDoc
*/
public function urlTemporaryCredentials()
{
return 'https://api.twitter.com/oauth/request_token';
$url = 'https://api.twitter.com/oauth/request_token';
$queryParams = $this->temporaryCredentialsQueryParameters();

return empty($queryParams) ? $url : $url . '?' . $queryParams;
}

/**
Expand Down Expand Up @@ -97,4 +143,40 @@ public function userScreenName($data, TokenCredentials $tokenCredentials)
{
return $data['name'];
}

/**
* Query parameters for a Twitter OAuth request to get temporary credentials.
*
* @return string
*/
protected function temporaryCredentialsQueryParameters()
{
$queryParams = [];

if ($scope = $this->getApplicationScope()) {
$queryParams['x_auth_access_type'] = $scope;
}

return http_build_query($queryParams);
}

/**
* Parse configuration array to set attributes.
*
* @param array $configuration
*
* @return void
*/
private function parseConfiguration(array $configuration = [])
{
$configToPropertyMap = [
'scope' => 'applicationScope',
];

foreach ($configToPropertyMap as $config => $property) {
if (isset($configuration[$config])) {
$this->$property = $configuration[$config];
}
}
}
}

0 comments on commit 0a423aa

Please sign in to comment.