使用WebClient验证Google帐户

时间:2012-07-01 16:05:11

标签: c# webclient

我正在尝试使用WebClient验证Google帐户。

class PostDataBuilder 
{
    private static Dictionary<string, string> 
        ToPropertyDictionary(object data) 
    { 
        var values = data
            .GetType()
            .GetProperties()
            .Select(x => new { 
                                Key = x.Name, 
                                Value = x.GetValue(data, null) 
                             });

        var result = new Dictionary<string, string>();
        foreach (var item in values)
            result.Add(item.Key, item.Value.ToString());

        return result;
    }

    public static string Build(object data)
    {
        string result = "";
        var dict = ToPropertyDictionary(data);
        foreach (var name in dict.Keys)
            result += name + "=" + HttpUtility.UrlEncode(dict[name]) + "&";
        return result.Substring(0, result.Length - 1);
    }
}

class Program
{
    static void Main(string[] args)
    {
        string postText = PostDataBuilder.Build(
        new
        {
            dsh = "-1903339439726094408",
            GALX = "-Ggrv6gqusk",
            timeStmp = "",
            secTok = "",
            Email = "WrongEmail@gmail.com",
            Passwd = "WrongPassword",
            signIn = "?????",
            rmShown = "1"
        });

        byte[] data = Encoding.UTF8.GetBytes(postText);

        WebClient wc = new WebClient();
        byte[] result = wc.UploadData(
            new Uri("https://accounts.google.com/ServiceLoginAuth"), 
            "POST", data);

        string resultText = Encoding.UTF8.GetString(result);
    }
}

ResultText变量已设置,即使数据正确也是如此。怎么了?

1 个答案:

答案 0 :(得分:1)

您不应该使用Google等登录服务或试图伪造浏览器。最后它可能被认为是企图黑客攻击或其他任何东西,它很可能在下次更新页面时中断(或者甚至只是因为你的IP更改)。

而是按照here所述使用OpenID或OAuth。