1

First time trying to get Twitter API to work. I followed these instructions: https://developer.twitter.com/en/docs/twitter-api/tweets/search/quick-start/recent-search

I got my API Key and secret and bearer token. In postman I tried: https://api.twitter.com/2/tweets/search/recent?query=from:taylorswift13&tweet.fields=created_at&expansions=author_id&user.fields=description

But it returned nothing(I tried other username as well and less params), in authorization I picked bearer token and pasted it so I should be okay there(I tried entering a wrong token and there it told me authentication error so I believe the authentication part is fine). I tried to do the API key one and it also didn't work. Any help is greatly appreciated! enter image description here

Ikura
  • 188
  • 1
  • 11

1 Answers1

1

You needs to remove the from: in query value.

The token got it correctly.

From

from:taylorswift13

To

taylorswift13

So The recent search URL is

https://api.twitter.com/2/tweets/search/recent?tweet.fields=created_at&expansions=author_id&user.fields=description&query=taylorswift13

enter image description here

Get Access Token

https://api.twitter.com/oauth2/token?grant_type=client_credentials

enter image description here

This API can get the user name by user ID

https://api.twitter.com/2/users/{user-id}

enter image description here

This code will help to get all of tweets for specific user by name

import tweepy

bearer_token ="your token"

client = tweepy.Client(bearer_token=bearer_token)

user_name="taylorswift13"
user = client.get_user(username=user_name)

user_id = user.data.id

tweets = client.get_users_tweets(id=user_id)
count = 0
for tweet in tweets.data:
    print(tweet.id, " -> ",tweet.text)
    count += 1
    print('---------------------------------------------------------', count)

next_token = tweets.meta['next_token']

while next_token != None:
    tweets = client.get_users_tweets(id=user_id, pagination_token=next_token)
    if tweets.data is not None:
        for tweet in tweets.data:
            print(tweet.id, " -> ",tweet.text)
            count += 1
            print('---------------------------------------------------------', count)
        if (tweets.meta['next_token']) is not None:
            next_token = tweets.meta['next_token']
        else:
            next_token = None
    else:
        next_token = None

It has a minor error but most tweet will display (total 570 tweets)

Error

    if (tweets.meta and tweets.meta['next_token']) is not None:
KeyError: 'next_toke

Result

... removed first lines

1140602666890014720  ->  A happy meal    https://xxx/hPAbOZEsKF
--------------------------------------------------------- 545
1140599648182243328  ->  Stan/Follow/Support! @A_doubleC_D @adamlambert @Adaripp @AdoreDelano @antoni @theebillyporter @bobbyberk @chesterlockhart @ciara #DeltaWork @DexStar84 @TheEllenShow @harto @HayleyKiyoko @QueenJadeJolie @jessetyler @JustinMikita @jvn @Karamo @katyperry @Lavernecox #YNTCDmusicvideo
--------------------------------------------------------- 546
1140594511325908992  ->  The #YNTCDmusicvideo is out! First, I want to say that my co-stars in this video are AMAZING. Please celebrate by supporting their work, following them, and going to see them perform. I’m SO grateful and SO EXCITED I ACTUALLY DO NEED TO CALM DOWN. https://xxx/787ksrnpBY https://xxx/Oj7GtQ7fxa
--------------------------------------------------------- 547
1140579059769978880  ->  The #YNTCDmusicvideo premieres in 1 hour 

Premiere watch page: https://xxx/gqUZ9FnKU1

@YouTube @youtubemusic https://xxx/Ek972t3hkE
--------------------------------------------------------- 548
1140424618202882053  ->  RT @TheEllenShow: My friend @TaylorSwift13 asked me to be in her new music video, “You Need To Calm Down.” It premieres tomorrow! Now I nee…
--------------------------------------------------------- 549
1140273222274945025  ->  Asked a few friends to be in the You Need To Calm Down video  Out tomorrow at 8:15am ET https://xxx/QFp2Ni4Lb0
--------------------------------------------------------- 550
1140025869706104840  ->  Tea time! Monday morning!  https://xxx/YLt8UD9dNC
--------------------------------------------------------- 551
1139939719113060352  ->  We all got crowns   https://xxx/vI2rXQQMTl
--------------------------------------------------------- 552
1139749718362398720  ->  Can you just not step on our gowns?  https://xxx/A8QPkZicqS
--------------------------------------------------------- 553
1139592994401771520  ->  RT @Target: We promise that you'll never find another like this! #PreorderLover @taylorswift13's new album - 4 exclusive deluxe versions. O…
--------------------------------------------------------- 554
1139585079418785792  ->  Alexa, play Today in Music 
@amazonmusic https://xxx/XNpxSYrPpG
--------------------------------------------------------- 555
1139562838891139079  ->  A delicious new video comes out Monday morning... https://xxx/fnZMz6P5dg
--------------------------------------------------------- 556
1139384677813219330  ->  Gxgjxkhdkdkydkhdkhfjvjfj

https://xxx/XjL0jBb4i0 https://xxx/8J6Bc89NQx
--------------------------------------------------------- 557
1139365833011015680  ->  There were five posts in the fence. https://xxx/dHgwKbd1Q5
--------------------------------------------------------- 558
1139282660860334107  ->  Lover, album out August 23. Cover shot by the artistic genius that is @valheria123  Pre-add, pre-save, pre-order (all the pre stuff you feel like doing) Can’t wait for you to hear this. https://xxx/SGjcCUYZdM https://xxx/IPy54raQUF
--------------------------------------------------------- 559
1138895438101323777  ->  Going live tomorrow on Instagram at 5pm ET  https://xxx/l4tnhj84tG
--------------------------------------------------------- 560
1135606264883482624  ->  RT @TheEllenShow: .@TaylorSwift13, you make me so proud. #PrideMonth https://xxx/nbVZoKOzub
--------------------------------------------------------- 561
1135606045378830337  ->  RT @onemuslimgal: Because you can want who you want. Boys and Boys. Girls and Girls. #letterToMysenator @taylornation13 @taylorswift13 http…
--------------------------------------------------------- 562
1135605849416753152  ->  RT @PetermanAudrey: Today I wrote to my senator, Ted Cruz, for the first time. Thanks to the awe inspiring words of Taylor Swift and the ex…
--------------------------------------------------------- 563
1135604986111508482  ->  RT @heartsandhalo: “The most common way people give up their power is by thinking they don’t have any.”  #lettertomysenator https://xxx/J…
--------------------------------------------------------- 564
1135604520338231296  ->  RT @HayleyGeronimo: It’s way past my bedtime, but I was so moved by Taylor’s letter to her senator that I had to pick up my own pen too!!…
--------------------------------------------------------- 565
1135601687274541057  ->  : @ashleyophoto, @jeffkravitz // Getty Entertainment https://xxx/mijbu6cV5E
--------------------------------------------------------- 566
1135217534825881600  ->  Like a rainbow with all of the colors

Thank you to everyone who came to Wango Tango! That was FUN  Ps a huge thank you to @brendonurie for surprising the crowd!!

: Rich Fury, Kevin Mazur, Jeff Kravitz, Wes and Alex // Getty Entertainment https://xxx/dXWewgIUPv
--------------------------------------------------------- 567
1134673128301453312  ->  #lettertomysenator
https://xxx/EKYMXZw5U9 https://xxx/Ym0mGeOHgc
--------------------------------------------------------- 568
1134524686388404225  ->  I love seeing what you’re listening to on your own playlists that you’ve created on @AppleMusic. Keep sharing them with me with the hashtag #PlaylistbyME https://xxx/ivvkvt6V9g https://xxx/9XfMvCNxia
--------------------------------------------------------- 569
1133936366977540096  ->   https://xxx/Lyr0vtXO69
--------------------------------------------------------- 570
Bench Vue
  • 5,257
  • 2
  • 10
  • 14
  • 1
    I thought the from would allow me to pick the twitter username(the author). When I do the query you have laid out it includes tweets with that username rather than set the author of it. But I'm assuming I need to figure out the author id and can then just use her author id – Ikura Jan 18 '23 at 03:23
  • Do you want to user name instead of author_id? – Bench Vue Jan 18 '23 at 03:35
  • I updated how to get `user name` by `user id` – Bench Vue Jan 18 '23 at 03:42
  • I wanted to get all the tweets Taylor Swift made(MOstl;y because I don't know people's user id but I know their username) – Ikura Jan 18 '23 at 05:19
  • 1
    Nevermind I figured it out, I needed to use timeline https://api.twitter.com/2/users/:id/tweets Thank you for all your help! – Ikura Jan 18 '23 at 05:31
  • 1
    Yes, This API get user id by user name https://api.twitter.com/2/users/by/username/taylorswift13 – Bench Vue Jan 18 '23 at 05:33
  • 1
    I updated my answer for getting all of tweets by `taylorswift13` user name. – Bench Vue Jan 18 '23 at 06:22