Created
May 21, 2024 00:04
-
-
Save rnelson/2a8c1456f8cd2ba215834d2a178b3511 to your computer and use it in GitHub Desktop.
Simple .NET 8 console app that sends a single tweet
This file contains hidden or 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.Text.Json; | |
using TwitterApiLibrary; | |
using TwitterApiLibrary.OAuth; | |
/* | |
* Credentials | |
* ----------- | |
* 1. Go to the Twitter Developer Portal and create a new application. | |
* 2. Edit the User authentication settings | |
* App permissions: Read and write | |
* Type of app: Web App, Automated App, or Bot | |
* Callback URI: http://localhost | |
* Website URL: http://example.com | |
* 3. Populate the API key and secret below, regenerating them on the | |
* "Keys and tokens" page if needed | |
* 4. Generate Access Token and Secret on the "Keys and tokens" page and | |
* populate below | |
*/ | |
const string TWEET_URL = "https://api.twitter.com/2/tweets"; | |
const string API_KEY = string.Empty; | |
const string API_KEY_SECRET = string.Empty; | |
const string ACCESS_TOKEN = string.Empty; | |
const string ACCESS_TOKEN_SECRET = string.Empty; | |
// Build the message. Obviously, add whatever logic you | |
// need to make this a fancy dynamic message. | |
var message = "This is my super awesome tweet."; | |
// Authenticate as a specific user, in this case the one | |
// owning the API app. Tweets will be sent from that user. | |
var auth = new SingleUserAuth | |
{ | |
ConsumerKey = API_KEY, | |
ConsumerSecret = API_KEY_SECRET, | |
AccessToken = ACCESS_TOKEN, | |
AccessTokenSecret = ACCESS_TOKEN_SECRET | |
}; | |
await auth.AuthorizeAsync(); | |
// Grab an OAuth header to put on our request | |
var authString = auth.GenerateAuthorizationString("POST", TWEET_URL); | |
// Build the POST request for /2/tweets (sans body) | |
var request = new RequestBuilder("POST", TWEET_URL); | |
request.AddHeader("Authorization", authString); | |
// Build the payload | |
var obj = new { text = message }; | |
var payload = JsonSerializer.Serialize(obj); | |
Console.WriteLine("Sending tweet..."); | |
var response = request.Send(payload, "application/json"); | |
Console.WriteLine(response); |
This file contains hidden or 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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net8.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="TwitterApiLibrary.Core" Version="0.0.9" /> | |
</ItemGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment