mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-03-30 13:32:16 +03:00
[TwitterBridge] Use Twitter API V1.1 guest/activate for requesting new guest tokens (#2414)
Instead of searching inside base html page for the guest token, this patch instead uses the Twitter API V1.1 to acquire guest tokens
This commit is contained in:
parent
084a1bcf19
commit
02a8ae4c62
1 changed files with 14 additions and 9 deletions
|
@ -545,7 +545,7 @@ EOD;
|
||||||
$guestToken = null;
|
$guestToken = null;
|
||||||
if($guestTokenUses === null || !is_array($guestTokenUses) || count($guestTokenUses) != 2
|
if($guestTokenUses === null || !is_array($guestTokenUses) || count($guestTokenUses) != 2
|
||||||
|| $guestTokenUses[0] <= 0 || (time() - $refresh) > self::GUEST_TOKEN_EXPIRY) {
|
|| $guestTokenUses[0] <= 0 || (time() - $refresh) > self::GUEST_TOKEN_EXPIRY) {
|
||||||
$guestToken = $this->getGuestToken();
|
$guestToken = $this->getGuestToken($apiKey);
|
||||||
if ($guestToken === null) {
|
if ($guestToken === null) {
|
||||||
if($guestTokenUses === null) {
|
if($guestTokenUses === null) {
|
||||||
returnServerError('Could not parse guest token');
|
returnServerError('Could not parse guest token');
|
||||||
|
@ -568,15 +568,20 @@ EOD;
|
||||||
|
|
||||||
// Get a guest token. This is different to an API key,
|
// Get a guest token. This is different to an API key,
|
||||||
// and it seems to change more regularly than the API key.
|
// and it seems to change more regularly than the API key.
|
||||||
private function getGuestToken() {
|
private function getGuestToken($apiKey) {
|
||||||
$pageContent = getContents('https://twitter.com', array(), array(), true);
|
$headers = array(
|
||||||
|
'authorization: Bearer ' . $apiKey,
|
||||||
|
);
|
||||||
|
$opts = array(
|
||||||
|
CURLOPT_POST => 1,
|
||||||
|
);
|
||||||
|
|
||||||
$guestTokenRegex = '/gt=([0-9]*)/m';
|
try {
|
||||||
preg_match_all($guestTokenRegex, $pageContent['header'], $guestTokenMatches, PREG_SET_ORDER, 0);
|
$pageContent = getContents('https://api.twitter.com/1.1/guest/activate.json', $headers, $opts, true);
|
||||||
if (!$guestTokenMatches)
|
$guestToken = json_decode($pageContent['content'])->guest_token;
|
||||||
preg_match_all($guestTokenRegex, $pageContent['content'], $guestTokenMatches, PREG_SET_ORDER, 0);
|
} catch (Exception $e) {
|
||||||
if (!$guestTokenMatches) return null;
|
$guestToken = null;
|
||||||
$guestToken = $guestTokenMatches[0][1];
|
}
|
||||||
return $guestToken;
|
return $guestToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue