获取雅虎邮箱联系方式 [英] Get yahoo mail Contact

查看:72
本文介绍了获取雅虎邮箱联系方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码来检索雅虎邮件ID的联系人,如果用户已经提供了雅虎邮件的UserId或密码,但这不起作用



如果你有关于此代码的任何想法或建议请转发给我。

I am using this code to Retrieve Contact of yahoo mail id if user has given UserId or password of yahoo mail, but this is not working

If you have any idea or suggestion about this code please forward me.

using System;  
using System.Collections.Specialized;  
using System.Net;  
using System.Text;  
using System.Text.RegularExpressions;  
  
namespace Gnilly.Syndication.Mail  
{  
    public class YahooExtract  
    {  
        private const string _addressBookUrl = "http://address.yahoo.com/yab/us/Yahoo_ab.csv?loc=us&.rand=1671497644&A=H&Yahoo_ab.csv";  
        private const string _authUrl = "https://login.yahoo.com/config/login?";  
        private const string _loginPage = "https://login.yahoo.com/config/login";  
        private const string _userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3";  
  
        public bool Extract( NetworkCredential credential, out MailContactList list )  
        {  
            bool result = false;  
  
            list = new MailContactList();  
  
            try  
            {  
                WebClient webClient = new WebClient();  
                webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;  
                webClient.Encoding = Encoding.UTF8;  
  
                byte[] firstResponse = webClient.DownloadData( _loginPage );  
                string firstRes = Encoding.UTF8.GetString( firstResponse );  
  
  
                NameValueCollection postToLogin = new NameValueCollection();  
                Regex regex = new Regex( "type=\"hidden\" name=\"(.*?)\" value=\"(.*?)\"", RegexOptions.IgnoreCase );  
                Match match = regex.Match( firstRes );  
                while ( match.Success )  
                {  
                    if ( match.Groups[ 0 ].Value.Length > 0 )  
                    {  
                        postToLogin.Add( match.Groups[ 1 ].Value, match.Groups[ 2 ].Value );  
                    }  
                    match = regex.Match( firstRes, match.Index + match.Length );  
                }  
  
  
                postToLogin.Add( ".save", "Sign In" );  
                postToLogin.Add( ".persistent", "y" );  
  
                string login = credential.UserName.Split( '@' )[ 0 ];  
                postToLogin.Add( "login", login );  
                postToLogin.Add( "passwd", credential.Password );  
  
                webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;  
                webClient.Headers[ HttpRequestHeader.Referer ] = _loginPage;  
                webClient.Encoding = Encoding.UTF8;  
                webClient.Headers[ HttpRequestHeader.Cookie ] = webClient.ResponseHeaders[ HttpResponseHeader.SetCookie ];  
  
                webClient.UploadValues( _authUrl, postToLogin );  
                string cookie = webClient.ResponseHeaders[ HttpResponseHeader.SetCookie ];  
  
                if ( string.IsNullOrEmpty( cookie ) )  
                {  
                    return false;  
                }  
  
                string newCookie = string.Empty;  
                string[] tmp1 = cookie.Split( ',' );  
                foreach ( string var in tmp1 )  
                {  
                    string[] tmp2 = var.Split( ';' );  
                    newCookie = String.IsNullOrEmpty( newCookie ) ? tmp2[ 0 ] : newCookie + ";" + tmp2[ 0 ];  
                }  
  
                // set login cookie  
                webClient.Headers[ HttpRequestHeader.Cookie ] = newCookie;  
                byte[] thirdResponse = webClient.DownloadData( _addressBookUrl );  
                string thirdRes = Encoding.UTF8.GetString( thirdResponse );  
  
                string crumb = string.Empty;  
                Regex regexCrumb = new Regex( "type=\"hidden\" name=\"\\.crumb\" id=\"crumb1\" value=\"(.*?)\"", RegexOptions.IgnoreCase );  
                match = regexCrumb.Match( thirdRes );  
                if ( match.Success && match.Groups[ 0 ].Value.Length > 0 )  
                {  
                    crumb = match.Groups[ 1 ].Value;  
                }  
  
  
                NameValueCollection postDataAB = new NameValueCollection();  
                postDataAB.Add( ".crumb", crumb );  
                postDataAB.Add( "vcp", "import_export" );  
                postDataAB.Add( "submit[action_export_yahoo]", "Export Now" );  
  
                webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;  
                webClient.Headers[ HttpRequestHeader.Referer ] = _addressBookUrl;  
  
                byte[] FourResponse = webClient.UploadValues( _addressBookUrl, postDataAB );  
                string csvData = Encoding.UTF8.GetString( FourResponse );  
  
                string[] lines = csvData.Split( '\n' );  
                foreach ( string line in lines )  
                {  
                    string[] items = line.Split( ',' );  
                    if ( items.Length < 5 )  
                    {  
                        continue;  
                    }  
                    string email = items[ 4 ];  
                    string name = items[ 3 ];  
                    if ( !string.IsNullOrEmpty( email ) && !string.IsNullOrEmpty( name ) )  
                    {  
                        email = email.Trim( '\"' );  
                        name = name.Trim( '\"' );  
                        if ( !email.Equals( "Email" ) && !name.Equals( "Nickname" ) )  
                        {  
                            MailContact mailContact = new MailContact();  
                            mailContact.Name = name;  
                            mailContact.Email = email;  
                            list.Add( mailContact );  
                        }  
                    }  
                }  
  
                result = true;  
            }  
            catch  
            {  
            }  
            return result;  
        }  
    }  
}

推荐答案

这段代码有一段时间...

问题是雅虎改变了合同,现在在将联系人输出到csv之前他们会问guptcha - 你必须从他们提供的图片中手动输入。

所以在你的代码中你模拟用户操作:

1.登录雅虎,

2.转到联系部分,

3.导出联系人,

4.阅读导出的联系人列表



除了现在在步骤2和3之间,你必须输入一些不能自动化的短语。



如果您添加行[保存网页输出并在浏览器中查看],您可以检查/调试您的代码:

获得后立即获取

This code goes around for some time...
The problem is that Yahoo changed contract and now before exporting contact to csv they ask guptcha - you have to enter manually word from the picture they provide.
So in your code you simulate user actions:
1. login into Yahoo,
2. go to contact section,
3. export contacts,
4. read exported contact list

Except now between steps 2 and 3 you have to type in some phrase, which cannot be automated.

You can check/debug your code if you add lines [to save web output and see it in browser]:
right after getting
string thirdRes = Encoding.UTF8.GetString( thirdResponse );

System.IO.File.WriteAllText ("c:/y3.html", thirdRes);
System.Diagnostics.Process.Start("c:/y3.html");





我认为,最初的想法是自动化用户操作, - 您可以尝试从您的雅虎页面导出雅虎联系人,现在您可以看到需要用户交互。

Yahoo提供了一些开放API,但它需要由雅虎创建的临时会话/令牌等 - 请参阅http://developer.yahoo.com/oauth/guide/oauth-auth-flow.html [ ^ ],http://developer.yahoo.com/social/rest_api_guide/contacts-resource.html [ ^ ]。我在这上面浪费了很多时间,无法使其发挥作用。有一些帖子,如 http://gnillydev.blogspot.com/2007 /10/yahoo-contact-import-class-in-c.html [ ^ ]

和其他人,但他们的代码现在都没有...



Original idea, i think, was to automate user actions, - you can try to export yahoo contacts from your yahoo page and now you can see that user interaction is needed.
Yahoo provides some open api, but it requires temp session/token created by Yahoo, etc - see http://developer.yahoo.com/oauth/guide/oauth-auth-flow.html[^] , http://developer.yahoo.com/social/rest_api_guide/contacts-resource.html[^]. I wasted a lot of time on this, cannot make it work. There are some posts like http://gnillydev.blogspot.com/2007/10/yahoo-contact-import-class-in-c.html[^]
and others, but none code from them is working right now...


这篇关于获取雅虎邮箱联系方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆