Dirk Nötzel
Dirk Nötzel

Reputation: 43

Problem with getting user.fields from Twitter API 2.0

I want to load tweets from Twitter API 2.0 endpoint and try to get the standard fields (author, text, ...) and some expansion field, esp. user.fields. Definition of endpoint and parameters works without errors. In the resulting json I find only the standard fields, but not the required user.fields (username, metrics).

Example code snippet:

from datetime import datetime, timedelta
import requests
import json
import pandas as pd

# read bearer token for authentication
with open('bearer_token.txt') as fp:
    BEARER_TOKEN = fp.read()

query_string = '("TSLA") (lang:en)'
    
# setup the API request
endpoint = 'https://api.twitter.com/2/tweets/search/recent'
headers = {'authorization': f'Bearer {BEARER_TOKEN}'}
params = {
    'query': query_string,
    'max_results': '100',
    'tweet.fields': 'created_at,lang,text,author_id,public_metrics',
    # that doesn't work
    'user.fields': 'name,username,public_metrics'
    #'expansions': 'attachments.media_keys'
}

response = requests.get(endpoint,
                            params=params,
                            headers=headers)  # send the request

print(json.dumps(response.json(), indent=3, sort_keys=True))

Resulting json shows this (where I missing the user fields: name,username,public_metrics):

    {
   "data": [
      {
         "author_id": "33286321",
         "created_at": "2021-03-13T16:25:02.000Z",
         "id": "1370772902769999874",
         "lang": "en",
         "public_metrics": {
            "like_count": 0,
            "quote_count": 0,
            "reply_count": 0,
            "retweet_count": 0
         },
         "text": "Try our Option Swing Trading service built for individuals who want to trade around their full-time careers. Take a free 10-day trail No Credit Card  $AAPL $TSLA $FB $MSFT"
      },
      {
         "author_id": "1142453041364385794",
         "created_at": "2021-03-13T16:24:41.000Z",
         "id": "1370772813469130756",
         "lang": "en",
         "public_metrics": {
            "like_count": 0,
            "quote_count": 0,
            "reply_count": 0,
            "retweet_count": 28
         },
         "text": "RT @Stalingrad_Poor: With bitcoin trading at $60k, TSLA has now made more money on its bitcoin investment and selling regulatory credits th\u2026"
      },
      {
         "author_id": "1349496824650989568",
         "created_at": "2021-03-13T16:24:35.000Z",
         "id": "1370772791411245056",
         "lang": "en",
         "public_metrics": {
            "like_count": 0,
            "quote_count": 0,
            "reply_count": 0,
            "retweet_count": 3
         },
         "text": "RT @VolaarRide: $OZSC By teaming up with the GEMM Network, we can now provide our customers with additional services such as Project Financ\u2026"
      },

Question: What do I have to do for getting these infos in my response?

Upvotes: 2

Views: 1665

Answers (3)

Valentin Schmidt
Valentin Schmidt

Reputation: 523

for people still confused, be sure to access the includes object, in js this looks like this var includes = JSON.parse(response.getContentText()).includes;

Upvotes: 0

Dirk Nötzel
Dirk Nötzel

Reputation: 43

Here the adjusted code due to the answer from @Patriot.

from datetime import datetime, timedelta
import requests
import json
import pandas as pd

# read bearer token for authentication
with open('bearer_token.txt') as fp:
    BEARER_TOKEN = fp.read()

query_string = '("TSLA") (lang:en)'
    
# setup the API request
endpoint = 'https://api.twitter.com/2/tweets/search/recent'
headers = {'authorization': f'Bearer {BEARER_TOKEN}'}
params = {
    'query': query_string,
    'max_results': '100',
    'expansions': 'author_id',
    'tweet.fields': 'created_at,lang,text,author_id,public_metrics',
    'user.fields': 'name,username,public_metrics'
}

response = requests.get(endpoint,
                            params=params,
                            headers=headers)  # send the request

print(json.dumps(response.json(), indent=3, sort_keys=True))

Upvotes: 1

Eoan Ermine
Eoan Ermine

Reputation: 239

According to documentation, to use user.fields parameter, you must include the author_id in expansions parameter.

Upvotes: 4

Related Questions