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
Twitter login new API v2 #1373
Comments
Upgraded Twitter class by ChatGPT: <?php
namespace Hybridauth\Provider;
use Hybridauth\Adapter\OAuth1;
use Hybridauth\Data;
use Hybridauth\Exception\UnexpectedApiResponseException;
use Hybridauth\User;
class Twitter extends OAuth1
{
protected $apiBaseUrl = 'https://api.twitter.com/2/';
protected $requestTokenUrl = 'https://api.twitter.com/oauth/request_token';
protected $accessTokenUrl = 'https://api.twitter.com/oauth/access_token';
protected $apiDocumentation = 'https://developer.twitter.com/en/docs/authentication/oauth-1-0a/obtaining-user-access-tokens';
protected function getAuthorizeUrl($parameters = [])
{
return $this->apiBaseUrl . 'oauth/authorize';
}
public function getUserProfile()
{
$response = $this->apiRequest('users/self');
$data = new Data\Collection($response->json());
if (!$data->exists('data.id')) {
throw new UnexpectedApiResponseException('Provider API returned an unexpected response.');
}
$userProfile = new User\Profile();
$userProfile->identifier = $data->get('data.id');
$userProfile->displayName = $data->get('data.username');
$userProfile->description = $data->get('data.description');
$userProfile->firstName = $data->get('data.name');
$userProfile->email = $data->get('data.email');
$userProfile->emailVerified = $data->get('data.email');
$userProfile->webSiteURL = $data->get('data.url');
$userProfile->region = $data->get('data.location');
$userProfile->profileURL = $data->exists('data.username')
? ('https://twitter.com/' . $data->get('data.username'))
: '';
$userProfile->photoURL = $data->exists('data.profile_image_url')
? $data->get('data.profile_image_url')
: '';
$userProfile->data = [
'followed_by' => $data->get('data.public_metrics.followers_count'),
'follows' => $data->get('data.public_metrics.following_count'),
];
return $userProfile;
}
public function getUserContacts($parameters = [])
{
$parameters = ['pagination_token' => '-1'] + $parameters;
$response = $this->apiRequest('users/self/following', 'GET', $parameters);
$data = new Data\Collection($response->json());
if (!$data->exists('data')) {
throw new UnexpectedApiResponseException('Provider API returned an unexpected response.');
}
if ($data->filter('data')->isEmpty()) {
return [];
}
$contacts = [];
foreach ($data->get('data') as $item) {
$contacts[] = $this->fetchUserContact($item);
}
return $contacts;
}
protected function fetchUserContact($item)
{
$item = new Data\Collection($item);
$userContact = new User\Contact();
$userContact->identifier = $item->get('id');
$userContact->displayName = $item->get('name');
$userContact->photoURL = $item->get('profile_image_url');
$userContact->description = $item->get('description');
$userContact->profileURL = $item->exists('username')
? ('https://twitter.com/' . $item->get('username'))
: '';
return $userContact;
}
public function setUserStatus($status)
{
$response = $this->apiRequest('users/self/tweet', 'POST', ['text' => $status]);
return $response->getStatusCode() === 201;
}
} I didn't test it |
Does someone tested this? And should oauth1 not be oauth2 as version 2 used oauth2 imo... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feature:
Twitter recommend to use V2
Дзякую
The text was updated successfully, but these errors were encountered: