auth progress

This commit is contained in:
Vincent Cloutier 2022-11-25 10:30:06 -05:00
parent ac6ca2535c
commit 9b3e92e423
3 changed files with 14 additions and 13 deletions

View file

@ -11,7 +11,8 @@ namespace BirdsiteLive.Twitter.Tools
{
public interface ITwitterAuthenticationInitializer
{
String Token { get; }
String BearerToken { get; }
String GuestToken { get; }
Task EnsureAuthenticationIsInitialized();
}
@ -22,7 +23,10 @@ namespace BirdsiteLive.Twitter.Tools
private static bool _initialized;
private readonly HttpClient _httpClient = new HttpClient();
private String _token;
public String Token {
public String BearerToken {
get { return "AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw"; }
}
public String GuestToken {
get { return _token; }
}
@ -48,20 +52,16 @@ namespace BirdsiteLive.Twitter.Tools
try
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api.twitter.com/oauth2/token"))
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api.twitter.com/1.1/guest/activate.json"))
{
var base64authorization = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(_settings.ConsumerKey + ":" + _settings.ConsumerSecret));
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");
request.Content = new StringContent("grant_type=client_credentials");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
request.Headers.TryAddWithoutValidation("Authorization", $"Bearer " + BearerToken);
var httpResponse = await _httpClient.SendAsync(request);
var c = await httpResponse.Content.ReadAsStringAsync();
httpResponse.EnsureSuccessStatusCode();
var doc = JsonDocument.Parse(c);
_token = doc.RootElement.GetProperty("access_token").GetString();
_token = doc.RootElement.GetProperty("guest_token").GetString();
}
_initialized = true;
return;

View file

@ -53,7 +53,7 @@ namespace BirdsiteLive.Twitter
+ "?expansions=author_id,referenced_tweets.id,attachments.media_keys,entities.mentions.username,referenced_tweets.id.author_id&tweet.fields=id,created_at,text,author_id,in_reply_to_user_id,referenced_tweets,attachments,withheld,geo,entities,public_metrics,possibly_sensitive,source,lang,context_annotations,conversation_id,reply_settings&user.fields=id,created_at,name,username,protected,verified,withheld,profile_image_url,location,url,description,entities,pinned_tweet_id,public_metrics&media.fields=media_key,duration_ms,height,preview_image_url,type,url,width,public_metrics,alt_text,variants";
using (var request = new HttpRequestMessage(new HttpMethod("GET"), reqURL))
{
request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + _twitterAuthenticationInitializer.Token);
request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + _twitterAuthenticationInitializer.BearerToken);
var httpResponse = await _httpClient.SendAsync(request);
httpResponse.EnsureSuccessStatusCode();
@ -111,7 +111,7 @@ namespace BirdsiteLive.Twitter
{
using (var request = new HttpRequestMessage(new HttpMethod("GET"), reqURL))
{
request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + _twitterAuthenticationInitializer.Token);
request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + _twitterAuthenticationInitializer.BearerToken);
var httpResponse = await _httpClient.SendAsync(request);
httpResponse.EnsureSuccessStatusCode();

View file

@ -41,6 +41,7 @@ namespace BirdsiteLive.Twitter
}
public async Task<TwitterUser> GetUserAsync(string username)
{
await _twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized();
JsonDocument res;
try
@ -48,8 +49,8 @@ namespace BirdsiteLive.Twitter
using (var request = new HttpRequestMessage(new HttpMethod("GET"), endpoint.Replace("elonmusk", username)))
{
request.Headers.TryAddWithoutValidation("Authorization", "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA");
request.Headers.TryAddWithoutValidation("x-guest-token", "1595938298886758401");
request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + _twitterAuthenticationInitializer.BearerToken);
request.Headers.TryAddWithoutValidation("x-guest-token", _twitterAuthenticationInitializer.GuestToken);
request.Headers.TryAddWithoutValidation("Referer", "https://twitter.com/");
request.Headers.TryAddWithoutValidation("x-twitter-active-user", "yes");
var httpResponse = await _httpClient.SendAsync(request);