FrenkyB
FrenkyB

Reputation: 7197

Twitter API returns 401 - unauthorized

I am having problems with twitter API. All I want to achieve is that users will be able to login through twitter on my web site.

I am using this library: Twitter

Library works well for google and facebook, but with twitter I can't make it work.

I am receiving:

System.Net.WebException: The remote server returned an error: (401) Unauthorized.

What I've already tried:

Set access token on twitter developers console and pasted this access token to header.

I've followed instructions from this link on twitter and created 'Authorization' header. It doesn't work, exception is still 401 - not authorized.

The one thing that I don't understand is in this link.

The explanation for 401 error is:

Authentication credentials were missing or incorrect. Also returned in other circumstances, for example all calls to API v1 endpoints now return 401 (use API v1.1 instead).

So, all calls for API v1 will return 401. 'Use API v1.1 instead' - how to do this? Where is the setting to use v1.1 instead of v1 ?

Here is method from original code, with my modifications - adding 'Authorization' header:

namespace Oauth2Login.Core
{
    public class RestfullRequest
    {
        public static string Request(string url, string method, string contentType, NameValueCollection authorizationHeader,
            string data, string proxyAddress = null)
        {
            var request = (HttpWebRequest) WebRequest.Create(url);
            request.Method = method;
            if (!string.IsNullOrEmpty(contentType))
                request.ContentType = contentType;

            string authHeaderString = "OAuth ";
            authHeaderString += PercentEncode("oauth_consumer_key") + "=\"" + (authorizationHeader.GetValues("oauth_consumer_key"))[0] + "\", ";
            authHeaderString += PercentEncode("oauth_nonce") + "=\"" + PercentEncode((authorizationHeader.GetValues("oauth_nonce"))[0]) + "\", ";
            authHeaderString += PercentEncode("oauth_signature") + "=\"" + PercentEncode((authorizationHeader.GetValues("oauth_signature"))[0]) + "\", ";
            authHeaderString += PercentEncode("oauth_signature_method") + "=\"" + PercentEncode((authorizationHeader.GetValues("oauth_signature_method"))[0]) + "\", ";
            authHeaderString += PercentEncode("oauth_timestamp") + "=\"" + PercentEncode((authorizationHeader.GetValues("oauth_timestamp"))[0]) + "\", ";
            authHeaderString += PercentEncode("oauth_token") + "=\"" + PercentEncode((authorizationHeader.GetValues("oauth_token"))[0]) + "\", ";
            authHeaderString += PercentEncode("oauth_version") + "=\"" + PercentEncode((authorizationHeader.GetValues("oauth_version"))[0]) + "\"";

            request.Headers.Add("Authorization", authHeaderString);

            if (!string.IsNullOrEmpty(proxyAddress))
            {
                IWebProxy proxy = new WebProxy(proxyAddress);
                proxy.Credentials = new NetworkCredential();
                request.Proxy = proxy;
            }

            if (!string.IsNullOrEmpty(data))
            {
                using (var swt = new StreamWriter(request.GetRequestStream()))
                {
                    swt.Write(data);
                }
            }

            string result = string.Empty;

            /*this line throws 401 - not authorized exception*/
            using (WebResponse response = request.GetResponse())
            {
                using (var sr = new StreamReader(response.GetResponseStream()))
                {
                    result = sr.ReadToEnd();
                }
            }
            return result;
        }

        public static string PercentEncode(string source)
        {
            return source
                .Replace(" ", "%20").Replace("!", "%21").Replace("&", "%26")
                .Replace(@"/", "%2F").Replace("=", "%3D").Replace("+", "%2B")
                .Replace(",", "%2C").Replace("-", "%2D").Replace(".", "%2E");
        }        
    }
}

Upvotes: 0

Views: 1116

Answers (1)

Erik
Erik

Reputation: 26

Maybe this might help you:

How to use cursors to iterate through the result set of GET followers/list in Twitter API using c#?

Check the part until the comment '//now we have been granted access...' in the private void StartCreateCall() method.

Upvotes: 0

Related Questions