Skip to content

Commit

Permalink
Annict APIへのリクエストにUA付けるように silane#7
Browse files Browse the repository at this point in the history
  • Loading branch information
noriokun4649 committed Feb 18, 2021
1 parent 7204e0d commit 212602b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions TVTComment/Model/TwitterUtils/AnnictUtils/AnnictApis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;

Expand All @@ -13,17 +14,22 @@ namespace TVTComment.Model.TwitterUtils.AnnictUtils
class AnnictApis
{
private readonly string HOST = $"https://api.annict.com/";
private readonly string UA;
private readonly string ACCESSTOKEN;

public AnnictApis(string accsestoken)
{
ACCESSTOKEN = accsestoken;
var version = Assembly.GetExecutingAssembly().GetName().Version.ToString(3);
UA = $"TvtComment/{version}";
}

public async Task<string> GetTwitterHashtagAsync(string title)
{
const string endpoint = "v1/works";
using var client = new HttpClient();
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", UA);

Stream stream;
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
Expand All @@ -15,6 +16,7 @@ class AnnictAuthentication
private readonly string ResponseType = "code";
private readonly string RedirectUri = "urn:ietf:wg:oauth:2.0:oob";
private readonly string Scope = "read";
private readonly string UA;
private readonly string ClientId;
private readonly string ClientSecret;

Expand All @@ -23,6 +25,8 @@ public AnnictAuthentication(string clientId, string clientSecret)
{
ClientId = clientId;
ClientSecret = clientSecret;
var version = Assembly.GetExecutingAssembly().GetName().Version.ToString(3);
UA = $"TvtComment/{version}";
}

public Uri GetAuthorizeUri()
Expand All @@ -35,6 +39,7 @@ public async Task<string> GetTokenAsync(string token)
{
const string endpoint = "oauth/token";
using var web = new WebClient();
web.Headers.Add("User-Agent", UA);
var postBodyParameters = new NameValueCollection();
postBodyParameters.Add("client_id", ClientId);
postBodyParameters.Add("client_secret", ClientSecret);
Expand Down

0 comments on commit 212602b

Please sign in to comment.