Skip to content

Instantly share code, notes, and snippets.

@SnowCait
Created July 23, 2019 01:56
Show Gist options
  • Select an option

  • Save SnowCait/b62aab2be5e8538c8e1ee9be032f97d4 to your computer and use it in GitHub Desktop.

Select an option

Save SnowCait/b62aab2be5e8538c8e1ee9be032f97d4 to your computer and use it in GitHub Desktop.
Twitter API over .NET Standard
// Twitter API over .NET Standard
//
// using OAuthStandard https://github.com/cjhoward92/OAuthStandard
// using HttpClient https://docs.microsoft.com/ja-jp/dotnet/api/system.net.http.httpclient?view=netstandard-2.0
// using .NET Core 3.0
// using C# 8.0
//
// NuGet
// PM> Install-Package OAuthStandard -Version 1.0.0
using OAuth;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
private static readonly HttpClient httpClient = new HttpClient();
static async Task Main(string[] args)
{
var consumerKey = "";
var consumerSecret = "";
var accessToken = "";
var accessTokenSecret = "";
var url = "https://api.twitter.com/1.1/statuses/home_timeline.json";
var client = OAuthRequest.ForProtectedResource(HttpMethod.Get.ToString(), consumerKey, consumerSecret, accessToken, accessTokenSecret);
client.RequestUrl = url;
var auth = client.GetAuthorizationHeader();
using var request = new HttpRequestMessage(HttpMethod.Get, client.RequestUrl);
using var content = new StringContent("", Encoding.UTF8, "application/json");
request.Headers.Authorization = new AuthenticationHeaderValue("OAuth", auth.Substring("OAuth ".Length));
request.Content = content;
using var response = await httpClient.SendAsync(request).ConfigureAwait(false);
var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Console.WriteLine(json);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment