Twitter-API-v2-sample-code icon indicating copy to clipboard operation
Twitter-API-v2-sample-code copied to clipboard

GET /2/users/by/username/:username/following endpoint returns 404 error

Open mehrdad-ataee opened this issue 2 years ago • 3 comments

Describe the bug In the dashboard, there are two endpoints given to getting the followings of an account, one is by using the user's ID <GET /2/users/:id/following> and the other is by using the user's name <GET /2/users/by/username/:username/following> the one with ID works, however, the one with username return 404 in response

To Reproduce Run this code:

import requests
import json

bearer_token = "{YOUR TOKEN}"

def bearer_oauth(r):
    r.headers["Authorization"] = f"Bearer {bearer_token}"
    r.headers["User-Agent"] = "v2FullArchiveSearchPython"
    return r

def connect_to_endpoint(url, params, method: str = 'GET'):
    response = requests.request(method, url, auth=bearer_oauth, params=params)
    print(response.status_code)
    if response.status_code != 200:
        raise Exception(response.status_code, response.text)
    return response.json()

def get_acc_followings(user_name: str):
    search_url = "https://api.twitter.com/2/users/by/username/%s/following" % user_name
    print(search_url)
    query_params = {"user.fields": "id,name,public_metrics,username"}
    json_response = connect_to_endpoint(search_url, query_params)
    print(json.dumps(json_response, indent=4, sort_keys=True))

if __name__ == "__main__":
    get_acc_followings('{PUT USERNAME HERE}')

Expected behavior The response should include details of "following" users

Additional context Running the same code by using the user's ID works just fine.

def get_acc_followings_by_id(user_id: str):
    search_url = "https://api.twitter.com/2/users/%s/following" % user_id
    print(search_url)
    query_params = {"user.fields": "id,name,public_metrics,username"}
    json_response = connect_to_endpoint(search_url, query_params)
    print(json.dumps(json_response, indent=4, sort_keys=True))

mehrdad-ataee avatar Jul 04 '22 08:07 mehrdad-ataee

Can you provide a link to where you're seeing GET /2/users/by/username/:username/following? I'm not sure what "in the dashboard" refers to. thanks.

andypiper avatar Jul 08 '22 17:07 andypiper

This endpoint seems to be open now for Basic Access. I've tried a request and got a Not Found response. Also the docs are not updated to include it. Does it work ?

alxspectrum avatar Apr 11 '23 02:04 alxspectrum