How can I retrieve a user's Twitter handle after they have authenticated with my Flask app

I have a Flask website where users login with their Twitter account. Upon logging in I am met with a few variables.

{
“access_token”:
“expires_at”:
“expires_in”:
“id_token”:
“scope”:
“token_type”:
“userinfo”: {
“aud”:
“exp”:
“iat”:
“iss”:
“name”:
“nickname”:
“nonce”:
“picture”:
“sid”:
“sub”:
“updated_at”:
}
}

My name and nickname variable are the same. How can I acquire the user’s Twitter handle so that I may display it upon login?

If it helps to know, the only code I have is the downloadable sample code for Python that Auth0 provides.

Hi @trapkingsam999,

Welcome to the Auth0 Community!

When using the Twitter (X) social connection, the basic profile attributes are required by default, which includes the user’s Twitter screen name.

Given that, you should be able to see the user’s Twitter handle on their user profile.

You should also consider switching on the Sync user profile attributes at each login toggle in the Twitter connection advanced settings.

Thanks,
Rueben

Thanks Rueben.

Unfortunately the user’s screen name is not presented in the JSON data. Sync user attributes at each login is toggled on. Using session.userinfo.screen_name retrieves nothing.

So, specifically, how can I acquire the logged-in user’s Twitter handle?

May I please get a reply on this subject? I had the exact same issue one year ago and quit, only to pick it up again now.

I have downloaded the sample Python code which Auth0 provides. I have successfully connected my Twitter app to Auth0. I can login to my website using my Twitter account without failure. The sync user attributes at each login is toggled on.

In my original post I shared with you all of the data which is presented to me upon logging in. The name and nickname variables are exactly the same. My Twitter handle is nowhere to be found in the provided JSON data. When I go to my HTML template and add the Jinja code {{session.userinfo.screen_name}} into an h1 element, nothing appears on the page. The same is true when I write {{session.user.screen_name}}, {{session.screen_name}}, and {{screen_name}}.

How can this possibly be so difficult? I am aware that I may be missing something, but I would really appreciate an answer because I feel like I have done everything right.

Hi @trapkingsam999,

Thanks for the update.

Can you confirm if you see the user’s screen name on their user profile on the Auth0 Dashboard > User Management > Users > Your User?

Thanks,
Rueben

Yes, when I navigate to each user I am able to see their Twitter handle under the variable screen_name.

How can I retrieve this data so that I may display it in my Flask template?

Hi @trapkingsam999,

You should be able to get that information by calling the Management API’s Get a user endpoint.

Thanks,
Rueben

Thanks for your insight, Rueben, I appreciate it.

I am a super noob, though. Does this mean I will have to manually insert the user’s ID into this endpoint documentation thing you referenced just to retrieve their Twitter handle? How can I achieve this automatically? Why isn’t the Twitter handle provided in the user info JSON dump that is displayed on the screen upon logging in?

Do you have any sources you could point me to in order to help me better understand? I just want the Twitter handle displayed on the Flask template after the user logs in.

I don’t get why the sample code which Auth0 provides can display everything but the user’s Twitter handle.

Hi @trapkingsam999,

Yes, that’s correct. You must supply the user_id to the Get a user endpoint to use it correctly. Once you make this request, the user’s profile is returned in the response, giving you the user’s Twitter screen name.

The user’s Twitter handle is a social identity provider basic attribute:
image

As I mentioned, if you want to get the user’s Twitter handle, you must use the https://auth0.com/docs/api/management/v2/users/get-users-by-id endpoint.

Thanks,
Rueben

Ok, thank you. I’ll try to write a script which automatically detects the user’s id then calls the API to then display their Twitter handle on the page.

Regarding the last sentence I wrote in the part of my reply you referenced, I know the basic profile button calls for the Twitter handle, but I don’t know why I have to manually write a script to get the Twitter handle when there is a lot of information that is already provided.

Here’s a screenshot of what I see when I login to my Flask website:

I don’t understand why the Twitter handle isn’t on the screen, but oh well!

1 Like

Hi @trapkingsam999,

Thanks for the response.

According to your screenshot, it looks like it’s returning information about your session. This includes your access token, ID token, and information derived from decoding the access token (expires_at, expires_in, scope), along with the "userinfo" section. The “userinfo” section comes from using the access token against the /userinfo endpoint of the Authentication API.

Essentially, the information displayed on that page does not have any details about the user’s full profile. As a result, you would need to write a script to get the Twitter screen name of the user.

I hope the explanation was helpful!

Thanks,
Rueben

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.