Skip to content

Commit

Permalink
fix(core): twitter api authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
LarchLiu committed Jul 5, 2023
1 parent 83fd8ef commit 22ebfc8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
57 changes: 57 additions & 0 deletions packages/webHub/src/website/twitter.com/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// https://github.com/zedeus/nitter/issues/919#issuecomment-1619067142
// https://git.sr.ht/~cloutier/bird.makeup/tree/087a8e3e98b642841dde84465c19121fc3b4c6ee/item/src/BirdsiteLive.Twitter/Tools/TwitterAuthenticationInitializer.cs#L36
export const tokens = [
'CjulERsDeqhhjSme66ECg:IQWdVyqFxghAtURHGeGiWAsmCAGmdW3WmbEx6Hck', // iPad
// valid, but endpoints differ
// 'IQKbtAYlXLripLGPWd0HUA:GgDYlkSvaPxGxC4X8liwpUoqKwwr3lCADbz8A7ADU', // iPhone
// '3nVuSoBZnx6U4vzUxf5w:Bcs59EFbbsdF6Sl9Ng71smgStWEGwXXKSjYvPVt7qys', // Android
// '3rJOl1ODzm9yZy63FACdg:5jPoQ5kQvMJFDYRNE8bQ4rHuds4xJqhvgNJM4awaE8', // Mac
]

const graphQLEndpointsPlain = [
'/graphql/oUZZZ8Oddwxs8Cd3iW3UEA/UserByScreenName',
'/graphql/3XDB26fBve-MmjHaWTUZxA/TweetDetail',
'/graphql/QqZBEqganhHwmU9QscmIug/UserTweets',
'/graphql/wxoVeDnl0mP7VLhe6mTOdg/UserTweetsAndReplies',
'/graphql/Az0-KW6F-FyYTc2OJmvUhg/UserMedia',
'/graphql/kgZtsNyE46T3JaEf2nF9vw/Likes',
// these endpoints are not available if authenticated as other clients
// FYI, endpoints for Android: https://gist.github.com/ScamCast/2e40befbd1b61c4a80cda2745d4df1f4
]

export const graphQLMap = Object.fromEntries(graphQLEndpointsPlain.map(endpoint => [endpoint.split('/')[3], endpoint]))

export const featuresMap = {
UserByScreenName: JSON.stringify({
hidden_profile_likes_enabled: false,
responsive_web_graphql_exclude_directive_enabled: true,
verified_phone_label_enabled: false,
subscriptions_verification_info_verified_since_enabled: true,
highlights_tweets_tab_ui_enabled: true,
creator_subscriptions_tweet_preview_api_enabled: true,
responsive_web_graphql_skip_user_profile_image_extensions_enabled: false,
responsive_web_graphql_timeline_navigation_enabled: true,
}),
UserTweets: JSON.stringify({
rweb_lists_timeline_redesign_enabled: true,
responsive_web_graphql_exclude_directive_enabled: true,
verified_phone_label_enabled: false,
creator_subscriptions_tweet_preview_api_enabled: true,
responsive_web_graphql_timeline_navigation_enabled: true,
responsive_web_graphql_skip_user_profile_image_extensions_enabled: false,
tweetypie_unmention_optimization_enabled: true,
responsive_web_edit_tweet_api_enabled: true,
graphql_is_translatable_rweb_tweet_is_translatable_enabled: true,
view_counts_everywhere_api_enabled: true,
longform_notetweets_consumption_enabled: true,
responsive_web_twitter_article_tweet_consumption_enabled: false,
tweet_awards_web_tipping_enabled: false,
freedom_of_speech_not_reach_fetch_enabled: true,
standardized_nudges_misinfo: true,
tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled: true,
longform_notetweets_rich_text_read_enabled: true,
longform_notetweets_inline_media_enabled: true,
responsive_web_media_download_video_enabled: false,
responsive_web_enhance_cards_enabled: false,
}),
}
26 changes: 23 additions & 3 deletions packages/webHub/src/website/twitter.com/twitterApi.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { errorMessage } from '@stargram/core/utils'
import { $fetch } from '@stargram/core'
import { featuresMap, graphQLMap, tokens } from './constants'

// https://github.com/mikf/gallery-dl/blob/a53cfc845e12d9e98fefd07e43ebffaec488c18f/gallery_dl/extractor/twitter.py#L716-L726
const headers = {
'authorization': 'Bearer AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw',
'authorization': '',
// Bearer AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw
// reference: https://github.com/dangeredwolf/FixTweet/blob/f3082bbb0d69798687481a605f6760b2eb7558e0/src/constants.ts#L23-L25
'x-guest-token': '',
Expand All @@ -12,8 +13,22 @@ const headers = {
// 'Referer': 'https://twitter.com/',
}

let tries = 0

async function newGuestToken() {
let guestToken = ''
// eslint-disable-next-line n/prefer-global/buffer
const authorization = `Basic ${Buffer.from(tokens[tries++ % tokens.length]).toString('base64')}`
const response = await $fetch(
'https://api.twitter.com/oauth2/token',
{
method: 'POST',
query: { grant_type: 'client_credentials' },
headers: {
Authorization: authorization,
},
})
headers.authorization = `${response.token_type} ${response.access_token}`
const activate = await $fetch(
'https://api.twitter.com/1.1/guest/activate.json',
{
Expand Down Expand Up @@ -56,9 +71,13 @@ async function paginationTweets(endpoint: string, userId: string, variables = {}
userId,
}

const { data } = await $fetch(`https://twitter.com/i/api${endpoint}?variables=${encodeURI(JSON.stringify(params))}`, {
const { data } = await $fetch(`https://twitter.com/i/api${endpoint}`, {
headers,
method: 'GET',
query: {
variables: JSON.stringify(params),
features: featuresMap.UserTweets,
},
})

if (data && Object.keys(data).length) {
Expand All @@ -81,7 +100,8 @@ async function paginationTweets(endpoint: string, userId: string, variables = {}
// https://github.com/mikf/gallery-dl/blob/a53cfc845e12d9e98fefd07e43ebffaec488c18f/gallery_dl/extractor/twitter.py#L795-L805
function tweetDetail(status: string) {
return paginationTweets(
'/graphql/ItejhtHVxU7ksltgMmyaLA/TweetDetail',
// '/graphql/ItejhtHVxU7ksltgMmyaLA/TweetDetail',
graphQLMap.TweetDetail,
'',
{
focalTweetId: status,
Expand Down

1 comment on commit 22ebfc8

@vercel
Copy link

@vercel vercel bot commented on 22ebfc8 Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

star-nexus – ./

star-nexus-larchliu.vercel.app
star-nexus.vercel.app
star-nexus-git-main-larchliu.vercel.app

Please sign in to comment.