简体   繁体   中英

C#: Error 403 while authenticating user on twitter C#

Hello I am making a GUI application on which a user gives his USER ID and Password to login on twitter . I have the following code but it's not working for me, giving an error of 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 + "!";

also attached the GUI picture 在此处输入图片说明 need help if anyone know the solution Thanks

Twitter no longer provide username/password access by default. To have access to such a feature you will have to contact them privately with a very strong case (and company) so that they authorize your app to use it. As an example the last company authorized to access this authentication was Instagram 2 years ago...

What you want to use is the OAuth mechanism to execute your query from an Application and User information.

I would suggest that you use the library that I developed for the Twitter developers using C# ( https://github.com/linvi/tweetinvi ).

You will be able to do what you want with the following lines of code :

Auth.SetUserCredentials("CONSUMER_KEY", "CONSUMER_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET");

var authenticatedUser = User.GetAuthenticatedUser();

label1.Text = "Welcome, " + authenticatedUser.Name + "!";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM