twitter (now X) api v2 doesn't return user's email #246

Closed
opened 2026-03-13 07:39:31 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @taehee-kim-sherpas on GitHub (Nov 19, 2024).

Describe the bug
we can't use twitter social login, because of email_not_found error.

f7287b02a1/packages/better-auth/src/social-providers/twitter.ts

here is their api document
https://developer.x.com/en/docs/x-api/users/lookup/api-reference/get-users-me#tab1

To Reproduce
Steps to reproduce the behavior:

  1. setup twitter oauth2 client_id and client_secret (see screenshot)
  2. Go to 'https://www.coaching-sospeso.org/auth/login'
  3. Click on '트위터로 계속하기' button (sky blue one with twitter bird icon)
  4. authorize our application
  5. redirect to https://www.coaching-sospeso.org/auth/login?error=email_not_found

and also i can't login with x(twitter) in better-auth demo app

Expected behavior
logged in well

Screenshots
here is our configuration. we 'request email from users'
image

Desktop (please complete the following information):

  • OS: macos 15.1, window10, ubuntu linux
  • Browser chrome
  • Version 130.0

Smartphone (please complete the following information):

  • Device: Android
  • OS: android 14
  • Browser chrome
  • Version 130.0

Additional context

I think we can't login with twitter v2 api when we need email as required.
https://devcommunity.x.com/t/how-to-get-email-in-twitter-v2-api/224225

Originally created by @taehee-kim-sherpas on GitHub (Nov 19, 2024). **Describe the bug** we can't use twitter social login, because of email_not_found error. https://github.com/better-auth/better-auth/blob/f7287b02a1e739ec3739a6d82053070e430c4245/packages/better-auth/src/social-providers/twitter.ts here is their api document https://developer.x.com/en/docs/x-api/users/lookup/api-reference/get-users-me#tab1 **To Reproduce** Steps to reproduce the behavior: 0. setup twitter oauth2 client_id and client_secret (see screenshot) 1. Go to 'https://www.coaching-sospeso.org/auth/login' 2. Click on '트위터로 계속하기' button (sky blue one with twitter bird icon) 3. authorize our application 4. redirect to https://www.coaching-sospeso.org/auth/login?error=email_not_found and also i can't login with x(twitter) in [better-auth demo app](https://demo.better-auth.com/sign-in) **Expected behavior** logged in well **Screenshots** here is our configuration. we 'request email from users' <img width="487" alt="image" src="https://github.com/user-attachments/assets/20eb4b04-fed5-41c2-9f0a-4fc993738b7b"> **Desktop (please complete the following information):** - OS: macos 15.1, window10, ubuntu linux - Browser chrome - Version 130.0 **Smartphone (please complete the following information):** - Device: Android - OS: android 14 - Browser chrome - Version 130.0 **Additional context** I think we can't login with twitter v2 api when we need email as required. https://devcommunity.x.com/t/how-to-get-email-in-twitter-v2-api/224225
Author
Owner

@LissajousOrbit commented on GitHub (Nov 26, 2024):

I'm facing same issue. The v2 api does not return email.
I think the problem is inside the social-providers/twitter.ts file.
They noted on line 14 that Twitter does not support email, but on line 147, it still returns an email value (which is null).
I think changing it to username might solve the problem.

// /src/social-providers/twitter.ts
// line 127
		async getUserInfo(token) {
			if (options.getUserInfo) {
				return options.getUserInfo(token);
			}
			const { data: profile, error } = await betterFetch<TwitterProfile>(
				"https://api.x.com/2/users/me?user.fields=profile_image_url",
				{
					method: "GET",
					headers: {
						Authorization: `Bearer ${token.accessToken}`,
					},
				},
			);
			if (error) {
				return null;
			}
			return {
				user: {
					id: profile.data.id,
					name: profile.data.name,
					//email: profile.data.email || null,
					email: profile.data.username,
					image: profile.data.profile_image_url,
					emailVerified: profile.data.verified || false,
				},
				data: profile,
			};
		},
@LissajousOrbit commented on GitHub (Nov 26, 2024): I'm facing same issue. The v2 api does not return email. I think the problem is inside the [`social-providers/twitter.ts`](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/social-providers/twitter.ts) file. They noted on line 14 that Twitter does not support email, but on line 147, it still returns an email value (which is null). I think changing it to username might solve the problem. ```javascript // /src/social-providers/twitter.ts // line 127 async getUserInfo(token) { if (options.getUserInfo) { return options.getUserInfo(token); } const { data: profile, error } = await betterFetch<TwitterProfile>( "https://api.x.com/2/users/me?user.fields=profile_image_url", { method: "GET", headers: { Authorization: `Bearer ${token.accessToken}`, }, }, ); if (error) { return null; } return { user: { id: profile.data.id, name: profile.data.name, //email: profile.data.email || null, email: profile.data.username, image: profile.data.profile_image_url, emailVerified: profile.data.verified || false, }, data: profile, }; }, ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#246