repl.it
@Kognise/

TwitterAPI

Nodejs

No description

fork
loading
Files
  • index.js
  • Packager files
  • package-lock.json
  • package.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const fetch = require('node-fetch')

const getUserInfo = async (screenName) => {
  const data = {
    screen_name: 'snigdhar0y',
    withHighlightedLabel: false
  }

  const url = [
    'https://api.twitter.com/graphql/P8ph10GzBbdMqWZxulqCfA/UserByScreenName',
    `?variables=${encodeURIComponent(JSON.stringify(data))}`
  ].join('')

  const res = await fetch(url, {
    headers: {
      'Authorization': `Bearer ${process.env.TWITTER_TOKEN}`
    }
  })
  const json = await res.json()

  return json.data.user
}

const getUserTweets = async (screenName) => {
  const userInfo = await getUserInfo(screenName)

  const url = [
    `https://api.twitter.com/2/timeline/profile/${userInfo.rest_id}.json`,
    '?include_profile_interstitial_type=1&include_blocking=1&include_blocked_by=1&include_followed_by=1&include_want_retweets=1&include_mute_edge=1&include_can_dm=1&include_can_media_tag=1&skip_status=1&cards_platform=Web-12&include_cards=1&include_composer_source=true&include_ext_alt_text=true&include_reply_count=1&tweet_mode=extended&include_entities=true&include_user_entities=true&include_ext_media_color=true&include_ext_media_availability=true&send_error_codes=true&simple_quoted_tweets=true&include_tweet_replies=false',
    `&userId=${userInfo.rest_id}`,
    '&count=20',
    '&ext=mediaStats%2CcameraMoment'
  ].join('')

  const res = await fetch(url, {
    headers: {
      'Authorization': `Bearer ${process.env.TWITTER_TOKEN}`,
      'Origin': 'https://twitter.com',
      'Referer': `https://twitter.com/${encodeURIComponent(screenName)}`
    }
  })
  const json = await res.json()

  console.log(json)

  return json.globalObjects.tweets
}

getUserTweets('snigdhar0y').then(console.log)
Fetching token
?