使用Twitter登录不会发送电子邮件

时间:2017-08-30 06:37:41

标签: c# asp.net twitter twitter-oauth twitter-login

我正在尝试使用C#在ASP.Net中实现Twitter登录。我想获取Twitter用户的注册电子邮件地址,但我的代码却出错:

  

解析Twitter响应时发生错误

这可能是因为 dataTable.Rows [0] [“email”]。ToString() 在这里无效。但是,其他细节正在完美呈现。以下是我的代码:

empty = "https://api.twitter.com/1.1/users/show.json";
str = twitterAuth.OAuthWebRequest(TwitterAuth.Method.GET, empty, string.Format("screen_name={0}", screenName));
dataTable = JsonStringToDataTable(str);
objTwitterBO.TwitterUserEmailId = dataTable.Rows[0]["email"].ToString();
objTwitterBO.TwitterUserID = dataTable.Rows[0]["Id"].ToString();
objTwitterBO.TwitterUserPhotoUrl = dataTable.Rows[0]["profile_image_url"].ToString();
objTwitterBO.TwitterUserName = dataTable.Rows[0]["name"].ToString();
objTwitterBO.TwitterUserScreenName = dataTable.Rows[0]["screen_name"].ToString();

OAuthWebRequest 的代码如下:

string empty = string.Empty;
      string str = string.Empty;
        string empty1 = string.Empty;
        if ((method == TwitterAuth.Method.POST || method == TwitterAuth.Method.DELETE ? true : method == TwitterAuth.Method.GET))
        {
            if (postData.Length > 0)
            {
                NameValueCollection nameValueCollection = HttpUtility.ParseQueryString(postData);
                postData = string.Empty;
                string[] allKeys = nameValueCollection.AllKeys;
                for (int i = 0; i < (int)allKeys.Length; i++)
                {
                    string str1 = allKeys[i];
                    if (postData.Length > 0)
                    {
                        postData = string.Concat(postData, "&");
                    }
                    nameValueCollection[str1] = HttpUtility.UrlDecode(nameValueCollection[str1]);
                    nameValueCollection[str1] = TwitterBase.UrlEncode(nameValueCollection[str1]);
                    postData = string.Concat(postData, str1, "=", nameValueCollection[str1]);
                }
                if (url.IndexOf("?") <= 0)
                {
                    url = string.Concat(url, "?");
                }
                else
                {
                    url = string.Concat(url, "&");
                }
                url = string.Concat(url, postData);
                string postDataplus = "&include_email=true";
                url = string.Concat(url, postDataplus);

            }
        }
        Uri uri = new Uri(url);
        string str2 = this.GenerateNonce();
        string str3 = this.GenerateTimeStamp();
        string str4 = base.GenerateSignature(uri, this.ConsumerKey, this.ConsumerSecret, this.Token, this.TokenSecret, this.CallBackUrl, this.OAuthVerifier, method.ToString(), str3, str2, out empty, out str);
        str = string.Concat(str, "&oauth_signature=", TwitterBase.UrlEncode(str4));
        if ((method == TwitterAuth.Method.POST ? true : method == TwitterAuth.Method.DELETE))
        {
            postData = str;
            str = string.Empty;
        }
        if (str.Length > 0)
        {
            empty = string.Concat(empty, "?");
        }
        empty1 = this.WebRequest(method, string.Concat(empty, str), postData);
        return empty1;

请告诉我如何使用此代码从twitter获取电子邮件地址。

1 个答案:

答案 0 :(得分:2)

用户/ show endpoint上没有该电子邮件地址。您需要使用validate_credentials端点。