Created
July 23, 2019 01:56
-
-
Save SnowCait/b62aab2be5e8538c8e1ee9be032f97d4 to your computer and use it in GitHub Desktop.
Twitter API over .NET Standard
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
| // 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