【问题标题】:C#: Error 403 while authenticating user on twitter C#C#:在 Twitter C# 上对用户进行身份验证时出现错误 403
【发布时间】:2016-10-01 07:12:30
【问题描述】:

您好,我正在制作一个 GUI 应用程序,用户在该应用程序上提供他的 用户 ID 和密码 以在 twitter 上登录。 我有以下代码,但它对我不起作用,给出 403 Forbidden

错误
StringBuilder sb = new StringBuilder();
            byte[] buf = new byte[8192];

            // encoding username/password
            string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(txtId.Text + ":" + txtPassword.Text));

            //connect with verify page

           HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.twitter.com/1.1/account/verify_credentials.xml");    
           // HttpWebRequest request =(HttpWebRequest)WebRequest.Create("http://twitter.com/account/verify_credentials.xml");                


            // setting method to GET- This API expects a GET method Call
            request.Method = "POST";

            // setting authorization levels
            request.Headers.Add("Authorization", "Basic " + user);

            request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            //request.ContentType = "application/x-www-form-urlencoded";

            // set the response from the GET command
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream resStream = response.GetResponseStream();

            // collect info returned from the GET call
            string tempStream = null; 
            int count = 0;

            do
            {
                count= resStream.Read(buf, 0, buf.Length);
                if(count!=0)
                {
                    tempStream= Encoding.ASCII.GetString(buf,0,count);
                    sb.Append(tempStream);
                }
            }while(count>0);

            //convert result to XML DOC
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(sb.ToString());

            // get person name from the newly created xml document
            XmlNodeList nodeList = doc.SelectNodes("/user/name");
            foreach (XmlNode node in nodeList)
                personName = node.InnerText;

            label1.Text = "Welcome, " + personName + "!";

还附上GUI图 如果有人知道解决方案,需要帮助 谢谢

【问题讨论】:

    标签: c# api ssl twitter


    【解决方案1】:

    默认情况下,Twitter 不再提供用户名/密码访问。 要访问此类功能,您必须私下与他们联系,并提供非常有力的案例(和公司),以便他们授权您的应用使用它。 例如,上一家获准访问此身份验证的公司是 2 年前的 Instagram...

    您要使用的是 OAuth 机制来从应用程序和用户信息执行查询。

    我建议您使用我为 Twitter 开发人员使用 C# (https://github.com/linvi/tweetinvi) 开发的库。

    您将能够使用以下代码行做您想做的事情:

    Auth.SetUserCredentials("CONSUMER_KEY", "CONSUMER_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET");
    
    var authenticatedUser = User.GetAuthenticatedUser();
    
    label1.Text = "Welcome, " + authenticatedUser.Name + "!";
    

    【讨论】:

    • 好的,谢谢您的信息...您能否详细说明如何在我的项目中使用该库以及我可以从哪里获得 consumer_key、c​​onsumer_secret、Access_token 和 Secret? ?
    • 我现在必须使用 Twitter API 做更多的工作。我需要你的帮助。请回复如果你能帮助我。请。谢谢
    • 是的,我绝对可以提供帮助。如果您对 Twitter 或 Tweetinvi 有任何疑问,您可以简单地使用 Tweetinvi 标签发布问题,我会回复。 (请注意,我将无法回复 github 上提到的 05/06 到 09/06。)
    • 好的,谢谢你这么多
    猜你喜欢
    • 2011-08-04
    • 2019-09-30
    • 2023-02-24
    • 2021-05-10
    • 1970-01-01
    • 2021-09-07
    • 2015-12-16
    • 2011-02-06
    • 1970-01-01
    相关资源
    最近更新 更多