Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing Twitter API to be compatible with OAuth2 from Hybriauth #64

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions includes/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ function uwp_social_build_provider_config( $provider )
if( $provider_key == "twitter" )
{
$config["providers"][$provider]["includeEmail"] = true;
$config["providers"][$provider]["scope"] = "users.read tweet.read offline.access";
$config["providers"][$provider]["authorize"] = true;
$config["providers"][$provider]['authorize_url_parameters']["code_challenge"] = 'challenge';
$config["providers"][$provider]['authorize_url_parameters']["code_challenge_method"] = 'plain';
}

$provider_scope = isset( $config["providers"][$provider]["scope"] ) ? $config["providers"][$provider]["scope"] : '' ;
Expand Down
2 changes: 1 addition & 1 deletion includes/social.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ function uwp_social_authenticated_process() {
die();
}


if ( $auth_mode == 'login' ) {

$data = uwp_social_get_user_data( $provider, $redirect_to );
Expand Down Expand Up @@ -480,6 +479,7 @@ function uwp_request_user_social_profile( $provider ) {

$config = uwp_get_provider_config_from_session_storage( $provider );


// if user authenticated successfully with social network
if ( $adapter->isConnected() ) {
// grab user profile via hybridauth api
Expand Down
2 changes: 1 addition & 1 deletion templates/bootstrap/social.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@
}

echo '</div>';
}
}
2 changes: 1 addition & 1 deletion templates/social.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@
box-shadow: none !important;
-moz-box-shadow: none !important; }</style>';

}
}
13 changes: 9 additions & 4 deletions vendor/hybridauth/Adapter/OAuth2.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ protected function initialize()
];

$refreshToken = $this->getStoredData('refresh_token');

if (!empty($refreshToken)) {
$this->tokenRefreshParameters = [
'grant_type' => 'refresh_token',
Expand All @@ -295,6 +296,7 @@ protected function initialize()
$this->apiRequestHeaders = [
'Authorization' => 'Bearer ' . $this->getStoredData('access_token')
];

}

/**
Expand All @@ -307,7 +309,6 @@ public function authenticate()
if ($this->isConnected()) {
return true;
}

try {
$this->authenticateCheckError();

Expand Down Expand Up @@ -420,6 +421,7 @@ protected function authenticateFinish()
*
* http://tools.ietf.org/html/rfc6749#section-4.1.1
*/

if ($this->supportRequestState
&& $this->getStoredData('authorization_state') != $state
) {
Expand All @@ -438,9 +440,7 @@ protected function authenticateFinish()
* http://tools.ietf.org/html/rfc6749#section-4.1.2
*/
$response = $this->exchangeCodeForAccessToken($code);

$this->validateAccessTokenExchange($response);

$this->initialize();
}

Expand Down Expand Up @@ -509,7 +509,12 @@ protected function getAuthorizeUrl($parameters = [])
*/
protected function exchangeCodeForAccessToken($code)
{
$this->tokenExchangeParameters['code'] = $code;
$this->tokenExchangeParameters['code'] = $code;
$this->tokenExchangeParameters['grant_type'] = 'authorization_code';
$this->tokenExchangeParameters['code_verifier'] = 'challenge';
$this->tokenExchangeHeaders['Authorization'] = 'Basic ' . base64_encode($this->clientId . ':' . $this->clientSecret);
$this->tokenExchangeHeaders['Content-Type'] = 'application/x-www-form-urlencoded';


$response = $this->httpClient->request(
$this->accessTokenUrl,
Expand Down
30 changes: 18 additions & 12 deletions vendor/hybridauth/Provider/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Hybridauth\Provider;

use Hybridauth\Adapter\OAuth1;
use Hybridauth\Adapter\OAuth2;
use Hybridauth\Exception\UnexpectedApiResponseException;
use Hybridauth\Data;
use Hybridauth\User;
Expand Down Expand Up @@ -38,12 +38,12 @@
* echo $e->getMessage() ;
* }
*/
class Twitter extends OAuth1
class Twitter extends OAuth2
{
/**
* {@inheritdoc}
*/
protected $apiBaseUrl = 'https://api.twitter.com/1.1/';
protected $apiBaseUrl = 'https://api.twitter.com/2/';

/**
* {@inheritdoc}
Expand All @@ -58,22 +58,27 @@ class Twitter extends OAuth1
/**
* {@inheritdoc}
*/
protected $accessTokenUrl = 'https://api.twitter.com/oauth/access_token';
protected $accessTokenUrl = 'https://api.twitter.com/2/oauth2/token';

/**
* {@inheritdoc}
*/
protected $apiDocumentation = 'https://dev.twitter.com/web/sign-in/implementing';

protected $tokenRefreshParameters = [

];
protected $tokenRefreshHeaders = [
];

/**
* {@inheritdoc}
*/
protected function getAuthorizeUrl($parameters = [])
{
if ($this->config->get('authorize') === true) {
$this->authorizeUrl = 'https://api.twitter.com/oauth/authorize';
$this->authorizeUrl = 'https://twitter.com/i/oauth2/authorize';
}

return parent::getAuthorizeUrl($parameters);
}

Expand All @@ -82,19 +87,18 @@ protected function getAuthorizeUrl($parameters = [])
*/
public function getUserProfile()
{
$response = $this->apiRequest('account/verify_credentials.json', 'GET', [
'include_email' => $this->config->get('include_email') === false ? 'false' : 'true',
]);
$response = $this->apiRequest('https://api.twitter.com/2/users/me', 'GET');

$data = new Data\Collection($response);
//This need to be fixed. Sending a formatted data is needed. Email may not be avilable.
$data = new Data\Collection($response->data);

if (!$data->exists('id_str')) {
if (!$data->exists('id')) {
throw new UnexpectedApiResponseException('Provider API returned an unexpected response.');
}

$userProfile = new User\Profile();

$userProfile->identifier = $data->get('id_str');
$userProfile->identifier = $data->get('id');
$userProfile->displayName = $data->get('screen_name');
$userProfile->description = $data->get('description');
$userProfile->firstName = $data->get('name');
Expand All @@ -118,6 +122,8 @@ public function getUserProfile()
'follows' => $data->get('friends_count'),
];

pre( $userProfile ); die;
wpdev10 marked this conversation as resolved.
Show resolved Hide resolved

return $userProfile;
}

Expand Down