Using twitter to get media token

I use the following code to return the carrier token, but I keep getting

"The remote server returned an error: (500) internal server error" on line "Response to WebResponse = request.GetResponse ();"

WebRequest request = WebRequest.Create("https://api.twitter.com/oauth2/token"); string consumerKey = "31111111111111111111"; string consumerSecret = "1111111111111111111111A"; string consumerKeyAndSecret = String.Format("{0}:{1}", consumerKey, consumerSecret); request.Method = "POST"; request.Headers.Add("Authorization", String.Format("Basic {0}", Convert.ToBase64String(Encoding.Unicode.GetBytes(consumerKeyAndSecret)))); request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; string postData = "grant_type=client_credentials"; byte[] byteArray = Encoding.UTF8.GetBytes(postData); request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); 

Any recommendation would be awesome

+7
source share
2 answers

In the past, I used TweetSharp, which uses the Twitter 1.1 API. You should probably use this for your twitter calls.

TweetSharp Github: https://github.com/danielcrenna/tweetsharp

If you need an example or what you need, let me know.

+1
source

I found a solution, spending a lot of time. This error will increase due to base64 encoding using Unicode. Just change UNICODE to UTF8 and nothing else.

End Code:

 WebRequest request = WebRequest.Create("https://api.twitter.com/oauth2/token"); string consumerKey = "31111111111111111111"; string consumerSecret = "1111111111111111111111A"; string consumerKeyAndSecret = String.Format("{0}:{1}", consumerKey, consumerSecret); request.Method = "POST"; request.Headers.Add("Authorization", String.Format("Basic {0}", Convert.ToBase64String(Encoding.UTF8.GetBytes(consumerKeyAndSecret)))); request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; string postData = "grant_type=client_credentials"; byte[] byteArray = Encoding.UTF8.GetBytes(postData); request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); 
+10
source

All Articles