Skip to content
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

User fields not returning any data #83

Open
1 task done
ThisModernDay opened this issue May 30, 2021 · 2 comments
Open
1 task done

User fields not returning any data #83

ThisModernDay opened this issue May 30, 2021 · 2 comments

Comments

@ThisModernDay
Copy link

ThisModernDay commented May 30, 2021

Prerequisites

  • Put an X between the brackets on this line if you have done all of the
    following:

Description

Attempting to get the username of a tweet author doesn't return information but also doesn't return an error

Steps to Reproduce

const params = {
        query:'"happiness is" -sad -worst -sucks -never -bad -disappointing -is:retweet lang:en -has:mentions -has:hashtags',
        expansions:'author_id',
        max_results:100,
        user:{
            fields: ['username']
        }
    }
const { data: tweets } = await client.get('tweets/search/recent', params)
console.log(tweets)

Expected behavior:

return user fields in console log

Actual behavior:

 {
    author_id: '731951078',
    id: '1398876897568776192',
    text: 'Happiness is the only option no matter what     What mean to be will 🙌🏻'
  },

Reproduces how often:

100%

Versions

latest

Additional Information

N/A

@ThisModernDay
Copy link
Author

I was able to resolve this by taking this approach

const params = {
        query:'"happiness is" -sad -worst -sucks -never -bad -http -https -disappointing -is:retweet lang:en -has:mentions -has:hashtags',
        expansions:'author_id',
        max_results:30,
        user:{
            fields: ['username', 'profile_image_url']
        }
    }
    const response = await client.get('tweets/search/recent', params)
    const tweets = response.data
    tweets.forEach((tweet, i) => {
        const user = response.includes.users[i]
        if(tweet.text.match(/^happiness is /i)){
            if(!tweet.text.includes('\n')){
                const fullTweet = {
                    tweet,
                    user
                }
                console.log(fullTweet)
            }
        }
    });

which gives the desired output

{
  tweet: {
    author_id: '1371394636846002181',
    id: '1398991509907677184',
    text: 'Happiness is not in the mere possession of money; it lies in the joy of achievement, in the thrill of creative effort.'
  },
  user: {
    profile_image_url: 'https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png',
    name: 'dfbe443',
    id: '1371394636846002181',
    username: 'dfbe443'
  }
}

however If this is the needed approach I feel as if it should be in the readme

@brandongregoryscott
Copy link
Contributor

@BrandonLeffew I believe this is the expected behavior of the Twitter V2 API itself. Almost all fields are now 'opt-in', so you no longer get more data than you request, unless you request it.

See The New Twitter API v2 > Fundamentals > Expansions

If you would like to request fields related to the user that posted that Tweet, or the media, poll, or place that was included in that Tweet, you will need to pass the related expansion query parameter in your request to receive that data in your response.

For reference, the code in your 'fixed' request that does this is:

user:{
    fields: ['username', 'profile_image_url']
}

You don't need to do the transformation from response.includes, but that's where the expansion data is returned. This is the sample response from expanding the media, Tweet and user objects (from the same page)

{
    "data": {
        "attachments": {
            "media_keys": [
                "16_1211797899316740096"
            ]
        },
        "author_id": "2244994945",
        "id": "1212092628029698048",
        "referenced_tweets": [
            {
                "type": "replied_to",
                "id": "1212092627178287104"
            }
        ],
        "text": "We believe the best future version of our API will come from building it with YOU. Here’s to another great year with everyone who builds on the Twitter platform. We can’t wait to continue working with you in the new year. https://t.co/yvxdK6aOo2"
    },
    "includes": {
        "media": [
            {
                "media_key": "16_1211797899316740096",
                "type": "animated_gif"
            }
        ],
        "users": [
            {
                "id": "2244994945",
                "name": "Twitter Dev",
                "username": "TwitterDev"
            }
        ],
        "tweets": [
            {
                "author_id": "2244994945",
                "id": "1212092627178287104",
                "referenced_tweets": [
                    {
                        "type": "replied_to",
                        "id": "1212092626247110657"
                    }
                ],
                "text": "These launches would not be possible without the feedback you provided along the way, so THANK YOU to everyone who has contributed your time and ideas. Have more feedback? Let us know ⬇️ https://t.co/Vxp4UKnuJ9"
            }
        ]
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants