Hello!

I’m trying to get geo informations in a request. But it returns a error every time i try to add a extention to get a more precise information. The code is:

def create_headers(bearer_token):
    headers = {"Authorization": "Bearer {}".format(bearer_token)}
    return headers


def connect_to_endpoint(url, headers, params):
    response = requests.request("GET", url, headers=headers, params=params)
    print(response.status_code)
    if response.status_code != 200:
        raise Exception(response.status_code, response.text)
    return response.json()


def set_user():
    user = 'pedro_drocha'
    return user


def set_params(now):
    user = set_user()
    query_params = {
        'start_time': '2010-12-21T13:00:00.00Z',
        'end_time': now,
        'max_results': '500',
        'query': f'from:{user}',
        'expantions': 'place.fields',
        'tweet.fields': 'author_id,created_at,in_reply_to_user_id,lang,public_metrics,entities,referenced_tweets,possibly_sensitive,geo',
        
        }
    return query_params



def main():
    pp = pprint.PrettyPrinter(indent=4)
    user = set_user()
    print(f"Coletando {user}")
    df = pd.DataFrame()
    dfs = []
    now = datetime.datetime.utcnow()
    menos_15segs = timedelta(seconds=15)
    now = now - menos_15segs
    now = now.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-4] + "Z"
    print(now)
    query_params = set_params(now)
    endpoint, consumer_key, consumer_secret, bearer_token = get_creds()
    headers = create_headers(bearer_token)
    json_response = connect_to_endpoint(endpoint, headers, query_params)

    # Quando se requisita informações que não vem por padrão no tweet, a resposta é um dicionário aninhado, que não é muito simples de se trabalhar
    
    todas_as_infos_dos_tweets = copy.deepcopy(json_response['data'])
    # Então fazemos uma cópia do dicionário, para não alterar a resposta original, e realizamos o achatamento/desaninhamento do dicionário pela função p_metrics 
    todas_as_infos_dos_tweets = p_metrics(todas_as_infos_dos_tweets)   
    dfs.append(pd.DataFrame(todas_as_infos_dos_tweets))

    while 'next_token' in json_response['meta'].keys():
        params = query_params
        params['next_token'] = json_response['meta']['next_token']
        json_response = connect_to_endpoint(endpoint, headers, query_params)
        todas_as_infos_dos_tweets = copy.deepcopy(json_response['data'])
        todas_as_infos_dos_tweets = p_metrics(todas_as_infos_dos_tweets)
        dfs.append(pd.DataFrame(todas_as_infos_dos_tweets))

    # print(json.dumps(json_response, indent=4, sort_keys=True))

    df = pd.concat(dfs, ignore_index=True)
    df.to_csv(f'{user}.csv', sep=';')


def p_metrics(infos):
    """Percorre a lista de dicionarios e retorna uma nova lista de dicionários desaninhados (flatten)"""
    novas_infos = []
    pp = pprint.PrettyPrinter(indent=4)      
    for tweet in infos:
        tweet = flatten(tweet, reducer='underscore')
        novas_infos.append(tweet)        
    return novas_infos
    
if __name__ == '__main__':
    main()

How may I use extentions to get the informations I need for geo?

The error result is:

Coletando pedro_drocha
2021-04-23T14:55:10.02Z
400
Traceback (most recent call last):
  File "main.py", line 94, in <module>
    main()
  File "main.py", line 61, in main
    json_response = connect_to_endpoint(endpoint, headers, query_params)
  File "main.py", line 22, in connect_to_endpoint
    raise Exception(response.status_code, response.text)
Exception: (400, '{"errors":[{"parameters":{"expantions":["place.fields"]},"message":"The query parameter [expantions] is not one of [query,start_time,end_time,since_id,until_id,max_results,next_token,expansions,tweet.fields,media.fields,poll.fields,place.fields,user.fields]"}],"title":"Invalid Request","detail":"One or more parameters to your request was invalid.","type":"https://api.twitter.com/2/problems/invalid-request"}')

The parameter name you want is expansions

And you should also specify place.fields.

I would recommend using twarc as a library to make these kinds of calls instead: twarc.Client2 - twarc You can use it use it as a command line tool too.

Also to get a CSV, use twarc-csv plugin GitHub - DocNow/twarc-csv: A plugin for twarc2 for converting tweet JSON into DataFrames and exporting to CSV.

Thank you!