I am trying to add a button to a desktop application that will post a picture to twitter. I have found some sample code online that uses ASP.net but it seems when I try to use it in a C# desktop application I'm getting an error with the Request and Response Names. Does anyone know why the Request and Response names would not cause any issues in ASP but would in C#. Do I need to create an httpRequest/Response?
Here is my code. Thanks for any help you can provide!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Twitterizer;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
namespace TwitterTestApp
{
class Twitter
{
public Twitter()
{
}
public void PostPicture()
{
string oauth_consumer_key = "123456789";
string oauth_consumer_secret = "abdefg123456";
if (Request["oauth_token"] == null)
{
OAuthTokenResponse reqToken = OAuthUtility.GetRequestToken(oauth_consumer_key, oauth_consumer_secret, Request.Url.AbsoluteUri);
Response.Redirect(string.Format("http://twitter.com/oauth/authorize?oauth_token={0}", reqToken.Token));
}
else
{
string requestToken = Request["oauth_token"].ToString();
string pin = Request["oauth_verifier"].ToString();
var tokens = OAuthUtility.GetAccessToken(oauth_consumer_key, oauth_consumer_secret, requestToken, pin);
OAuthTokens accesstoken = new OAuthTokens()
{
AccessToken = tokens.Token,
AccessTokenSecret = tokens.TokenSecret,
ConsumerKey = oauth_consumer_key,
ConsumerSecret = oauth_consumer_secret
};
byte[] photo = File.ReadAllBytes(@"C:\img.jpg");
TwitterResponse<TwitterStatus> response = TwitterStatus.UpdateWithMedia(accesstoken, "img", photo);
if (response.Result == RequestResult.Success)
{
Response.Write("This is YOUR PAGE");
}
else
{
Response.Write("Try some other time");
}
}
}
}
}
Request
and Response
are both supposed to be variables. When you copied this code, you may have not gotten it all. There should be definitions for both variables, but there isn't, so VS shows an error that they do not exist.
This code will not work out of the box in plain C#, as Request
and Response
are both variables in ASP.NET. Request is what the client sends to the webserver, and response is what it sends back.
EDIT: After looking at your code a bit more, this will not work at all. This is designed to be a page in an ASP.NET application. Your desktop application is not a server and has no reason to try and send a "response".
Look into WebRequest
, which you can use to send a POST request to the twitter API.