Created
January 8, 2020 16:37
-
-
Save mgroves/7230fdf63ddc66cb34c98f3258c32e48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using LinqToTwitter; | |
namespace TweetImage | |
{ | |
class Program | |
{ | |
static async Task Main(string[] args) | |
{ | |
var text = "Hey @csauer I sent this from a .NET console program! #codemash"; | |
var filename = "C:\\whatever\\path\\foo.jpg"; | |
var exists = File.Exists(filename); | |
if(!exists) | |
throw new FileNotFoundException($"Couldn't find {filename}!"); | |
await TweetIt(text, filename); | |
} | |
private static async Task TweetIt(string tweetText, string filename) | |
{ | |
var auth = new SingleUserAuthorizer | |
{ | |
CredentialStore = new SingleUserInMemoryCredentialStore | |
{ | |
ConsumerKey = "<your consumer key>", | |
ConsumerSecret = "<your consumer secret>", | |
AccessToken = "<your access token>", | |
AccessTokenSecret = "<your access token secret>" | |
} | |
}; | |
var twitterCtx = new TwitterContext(auth); | |
Media media = await twitterCtx.UploadMediaAsync(File.ReadAllBytes(filename), "image/jpeg", "tweet_image"); | |
Status tweet = await twitterCtx.TweetAsync(tweetText, new ulong[] { media.MediaID }); | |
twitterCtx.Dispose(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment