How to get the email address of twitter user with

2019-06-20 14:04发布

I want to put a plug in on my website for "login with twitter", and my website needs to get user's twitterID and email after user allows my twitter application to access their data. I looked through the twitter dev documents, but it seems always all about OAuth with complicated examples. Is there an easy way to do this? I already put the same kind of plugin for facebook, and it is very simple. Thanks!

8条回答
祖国的老花朵
2楼-- · 2019-06-20 14:34

I'm working on C#.Net and I used the Tweetinvi API, its easy to use and has a good documentation.

Well to answer your question, it's possible get the user's email and all the data.

1st: You need to create your Twitter app on "apps.twitter.com" and register it. Then in the app management you'll find a "Permission" tab and at the end of the page there is a checkbox that says if you need the email information. You'll have to provide a link to your Privacy Terms so the user can read and agree to share the information with your app, you can use 'https://google.com' but its recommended to provide a valid link.

And that's all. In the code, you just need to authenticate the user and get the data you need.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tweetinvi;
using Tweetinvi.Models;

namespace APIS
{
    public class TwitterConfig
    {
        IAuthenticationContext authenticationContext;

        /// <summary>
        /// Le indica a twitter que va a hacer una consulta de crendenciales. Esta consulta va a generar un PIN en caso de que sea correcto
        /// y el usuario tiene que digitar el pin.
        /// </summary>
        public void RequestForCredentials()
        {
            var appCredentials = new TwitterCredentials("CONSUMER-KEY", "CONSUMER-SECRET"); //GET THE CONSUMER INFORMATION ON THE TWITTER APP MANAGEMENT

            authenticationContext = AuthFlow.InitAuthentication(appCredentials);


            //This opens a web-browser and asks to the twitter client 
            //to accept the terms, and twitter gives a code and the user
            // have to introduce it on the app (use the AuthByPin(String pin) method )
            //Thats the PIN for auth the user and then get the data
            Process.Start(authenticationContext.AuthorizationURL);
        }

        /// <summary>
        /// Metodo que solicita un PIN que un usuario de Twitter al aceptar las condiciones se le brindó y este lo introdujo en la app
        /// para ser autenticado.
        /// </summary>
        /// <param name="pin">PIN del usuario</param>
        /// <returns>IAuthenticatedUser devuelve el usuario autenticado y con todos los datos.</returns>
        public IAuthenticatedUser AuthByPin(String pin) 
        {
            try
            {
                // Con este código PIN ahora es posible recuperar las credenciales de Twitter 
                var userCredentials = AuthFlow.CreateCredentialsFromVerifierCode(pin, authenticationContext);

                // Use las credenciales del usuario en su aplicación 
                Auth.SetCredentials(userCredentials);

                var user = User.GetAuthenticatedUser();
                return user; 
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw new ApplicationException("Problema al autenticar.", e);
            }
        }
    }
}
</code>

If you have some question ask me

查看更多
老娘就宠你
3楼-- · 2019-06-20 14:34

From the twitter api FAQ https://dev.twitter.com/docs/faq

How do I obtain a user's email address?

If you'd like a user's email address, you'll need to ask a user for it within the confines of your own application and service. The Twitter API does not provide the user's email address as part of the OAuth token negotiation process nor does it offer other means to obtain it.

查看更多
你好瞎i
4楼-- · 2019-06-20 14:36

Go to https://support.twitter.com/forms/platform Select "I need access to special permissions" Enter Application Name and ID. These can be obtained via https://apps.twitter.com/ -- the application ID is the numeric part in the browser's address bar after you click your app. Permissions Request: "Email address" Submit & wait for response After your request is granted, an addition permission setting is added in your twitter app's "Permission" section. Go to "Additional Permissions" and just tick the checkbox for "Request email addresses from users".

查看更多
Bombasti
5楼-- · 2019-06-20 14:36

There is NO way you can get email address of a twitter user. Twitter doesn't provide it : https://dev.twitter.com/docs/faq#6718

查看更多
男人必须洒脱
6楼-- · 2019-06-20 14:39

https://dev.twitter.com/rest/reference/get/account/verify_credentials Request a User’s Email Address

Requesting a user’s email address requires your application to be whitelisted by Twitter. To request access, please use this form.

Once whitelisted, the “Request email addresses from users” checkbox will be available under your app permissions on apps.twitter.com. Privacy Policy URL and Terms of Service URL fields will also be available under settings which are required for email access. If enabled, users will be informed via the oauth/authorize dialog that your app can access their email address.

查看更多
女痞
7楼-- · 2019-06-20 14:44

If twitter doesn't provide any API, then how does foursquare retrieve them?

  • Foursquare has provision to list out the user's foursquare-friends who have registered with twitter.
  • It also provides API which shows friends of users who commonly appear in 'foursquare friend's list' as well as 'twitter followers list' of the user.

In both the above cases comparison can be made only through email.

查看更多
登录 后发表回答