Using Oauth API 1.1 C#


#1

Hi I’m trying to update my application now I can’t use the old API.

Im trying to make a request to get a bearer token but always end up with a response code of 500. Please can anyone see if Im missing something as I was thinking it would be pretty straight forward.

 //Oauth Keys (Replace with values that are obtained from registering the application
        //https://dev.twitter.com/apps/new

        var oauth_consumer_key = "consumerkey goes here";
        var oauth_consumer_secret = "consumer secret here";

        //Token URL
        var oauth_url = "https://api.twitter.com/oauth2/token";


        var headerFormat = "Basic {0}";

        var authHeader = string.Format(headerFormat,
             Convert.ToBase64String(Encoding.Unicode.GetBytes(Uri.EscapeDataString(oauth_consumer_key) + ":" +
                    Uri.EscapeDataString((oauth_consumer_secret)))
                    ));

        var postBody = "grant_type=client_credentials";

        ServicePointManager.Expect100Continue = false;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(oauth_url);
        request.Headers.Add("Authorization", authHeader);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";

        using (Stream stream = request.GetRequestStream())
        {
            byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
            stream.Write(content, 0, content.Length);
        }

        request.Headers.Add("Accept-Encoding", "gzip");

        WebResponse response = request.GetResponse();   //Always Getting 500 Response Error

Thanks


#2

Have you tried using your same consumer key and secret in another pure HTTP tool, like curl on the command line? Nothing obvious stands out here as being incorrect. Are there any other things the HTTP client/library you’re using does implicitly?


#3

What if you replace this:

byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);

with this:

byte[] content = System.Text.Encoding.UTF8.GetBytes(postBody);


#4

Still 500’s.


#5

I get the same error. Doesn’t anybody have a working code sample?


#6

I figured it out. Joe was onto the solution. The problem is Encoding.Unicode.GetBytes. Use UTF8 instead.


#7

Still 500’s too :frowning:


#8

look at this

// https://dev.twitter.com/discussions/16281
// Using Oauth API 1.1 C#

        //Oauth Keys (Replace with values that are obtained from registering the application
        //https://dev.twitter.com/apps/new
var oauth_consumer_key = "xxxxxxxxxxxxx"; var oauth_consumer_secret = "xxxxxxxxxxxxxxxxxxxxxx";
        //Token URL
        var oauth_url = "https://api.twitter.com/oauth2/token";

        var headerFormat = "Basic {0}";
        var authHeader = string.Format(headerFormat,
        Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(oauth_consumer_key) + ":" +
                               Uri.EscapeDataString((oauth_consumer_secret)))));

        var postBody = "grant_type=client_credentials";
        ServicePointManager.Expect100Continue = false;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(oauth_url);
        request.Headers.Add("Authorization", authHeader);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";

        using (Stream stream = request.GetRequestStream())
        {
            byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
            stream.Write(content, 0, content.Length);
        }

        request.Headers.Add("Accept-Encoding", "gzip");
        WebResponse response = request.GetResponse();

#9

I get the same error.
Getting error at “Stream stream = request.GetRequestStream()”
‘response.GetResponseStream().Length’ threw an exception of type 'System.NotSupportedException’
Please help to figure it out.


#10

The problem was the encoding of the authHeader. This worked for me:

//Oauth Keys (Replace with values that are obtained from registering the application var oauth_consumer_key = m_ConsumerKey; var oauth_consumer_secret = m_ConsumerSecret;
        //Token URL
        var oauth_url = "https://api.twitter.com/oauth2/token";
        var headerFormat = "Basic {0}";
        var authHeader = string.Format(headerFormat,
                    Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(oauth_consumer_key) + ":" +
                    Uri.EscapeDataString((oauth_consumer_secret)))
                    ));

        var postBody = "grant_type=client_credentials";

        ServicePointManager.Expect100Continue = false;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(oauth_url);
        request.Headers.Add("Authorization", authHeader);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";

        using (Stream stream = request.GetRequestStream())
        {
            byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
            stream.Write(content, 0, content.Length);
        }

        request.Headers.Add("Accept-Encoding", "gzip");
        WebResponse response = request.GetResponse();

Make sure your auth header is UTF8, and your byte content stream is ASCII


#11

…for the auth header, yes


#12

Hey your code works great but the response I get back isn’t what I’m expecting as per the documentation… How did you parse the response?


#13

Have you got json string as response?


#14

Hey, are you using the oauth access token and outh token secret anywhere? I am getting response code 401- Unauthorized when I just have the above mentioned code. Any suggestions?

Thanks,


#15

Could you delete @tonyhamilt0n please