How do I tweet from C#


#1

I am using Linq2Twitter to post tweet to twitter. I am getting the error “Your credentials do not allow access to this resource”.

var auth = new ApplicationOnlyAuthorizer
{
Credentials = new InMemoryCredentials
{
ConsumerKey = ConfigurationManager.AppSettings[“oauth_consumer_key”],
AccessToken = ConfigurationManager.AppSettings[“oauth_token”],
ConsumerSecret = ConfigurationManager.AppSettings[“oauth_consumer_secret”],
OAuthToken = ConfigurationManager.AppSettings[“oauth_token”],
}
};
auth.Authorize();
var context = new TwitterContext(auth) {BaseUrl = “https://api.twitter.com/1/”};
context.UpdateStatus(“Hello world”);

This is the code I am using to post a tweet.

Please help me,. Its urgent for me.


#2

Hi,

Don’t specify the BaseUrl when instantiating TwitterContext. The one you’re using is for Twitter API v1.0, which is deprecated. TwitterContext will default to the proper BaseUrl for Twitter API v1.1, which is current. Here’s an example of using ApplicationOnlyAuthorizer:

var auth = new ApplicationOnlyAuthorizer
{
Credentials = new InMemoryCredentials
{
ConsumerKey =
ConfigurationManager.AppSettings[“twitterConsumerKey”],
ConsumerSecret =
ConfigurationManager.AppSettings[“twitterConsumerSecret”]
}
};

auth.Authorize();

var twitterCtx = new TwitterContext(auth);

var srch =
(from search in twitterCtx.Search
where search.Type == SearchType.Search &&
search.Query == "LINQ to Twitter"
select search)
.SingleOrDefault();

Console.WriteLine("\nQuery: {0}\n", srch.SearchMetaData.Query);
srch.Statuses.ForEach(entry =>
Console.WriteLine(
“ID: {0, -15}, Source: {1}\nContent: {2}\n”,
entry.StatusID, entry.Source, entry.Text));

This demo is part of the downloadable source code in the OAuthDemos.cs file of the LinqToTwitterDemos project.


#3

The reply doesn’t show an example of posting an update (rather than querying). Can you show a way that an application can post an update to Twitter without getting the error “Your credentials do not allow access to this resource”?


#4

?


#5

I believe you cannot tweet with an ApplicationOnlyAuthorizer. This is documented at https://dev.twitter.com/docs/auth/application-only-auth. The code should work if you switch to a SingleUserAuthorizer. This code works for me (when keys and secrets are populated).

    var auth=new SingleUserAuthorizer {
      CredentialStore=new SingleUserInMemoryCredentialStore {
        ConsumerKey="*****",
        ConsumerSecret="*****",
        AccessToken="*****",
        AccessTokenSecret="*****",
      }
    };

    auth.AuthorizeAsync().Wait();

    using(var context=new TwitterContext(auth)) {
      var tweet=context.TweetAsync("Hello world!").Result;
    }