1 // OpenTween - Client of Twitter
2 // Copyright (c) 2017 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
23 using System.Collections.Generic;
25 using System.Net.Http;
26 using System.Net.Http.Headers;
27 using System.Runtime.Serialization.Json;
29 using System.Text.RegularExpressions;
30 using System.Threading.Tasks;
32 using System.Xml.Linq;
33 using OpenTween.Connection;
35 namespace OpenTween.Api
39 public static readonly Uri ApiBase = new Uri("https://api-ssl.bitly.com/");
41 public string EndUserAccessToken { get; set; }
43 public string EndUserLoginName { get; set; }
44 public string EndUserApiKey { get; set; }
46 private HttpClient http => this.localHttpClient ?? Networking.Http;
47 private readonly HttpClient localHttpClient;
54 public BitlyApi(HttpClient http)
55 => this.localHttpClient = http;
57 public async Task<Uri> ShortenAsync(Uri srcUri, string domain = null)
59 var query = new Dictionary<string, string>
62 ["longUrl"] = srcUri.OriginalString,
65 if (!string.IsNullOrEmpty(domain))
66 query["domain"] = domain;
68 var uri = new Uri("/v3/shorten", UriKind.Relative);
69 var responseText = await this.GetAsync(uri, query).ConfigureAwait(false);
71 if (!Regex.IsMatch(responseText, @"^https?://"))
72 throw new WebApiException("Failed to create URL.", responseText);
74 return new Uri(responseText.TrimEnd());
77 public async Task<string> GetAsync(Uri endpoint, IEnumerable<KeyValuePair<string, string>> param)
79 var paramWithToken = param.Concat(this.CreateAccessTokenParams());
81 var requestUri = new Uri(new Uri(ApiBase, endpoint), "?" + MyCommon.BuildQueryString(paramWithToken));
83 using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri))
84 using (var response = await this.http.SendAsync(request).ConfigureAwait(false))
86 return await response.Content.ReadAsStringAsync()
87 .ConfigureAwait(false);
91 public async Task<string> GetAccessTokenAsync(string username, string password)
93 var param = new Dictionary<string, string>
95 ["grant_type"] = "password",
96 ["username"] = username,
97 ["password"] = password,
100 var endpoint = new Uri(ApiBase, "/oauth/access_token");
102 using (var request = new HttpRequestMessage(HttpMethod.Post, endpoint))
103 using (var postContent = new FormUrlEncodedContent(param))
105 var authzParam = ApplicationSettings.BitlyClientId + ":" + ApplicationSettings.BitlyClientSecret;
106 request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(authzParam)));
108 request.Content = postContent;
110 using (var response = await this.http.SendAsync(request).ConfigureAwait(false))
112 var responseBytes = await response.Content.ReadAsByteArrayAsync()
113 .ConfigureAwait(false);
115 return this.ParseOAuthCredential(responseBytes);
120 private string ParseOAuthCredential(byte[] responseBytes)
122 using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(responseBytes, XmlDictionaryReaderQuotas.Max))
124 var xElm = XElement.Load(jsonReader);
126 var statusCode = xElm.Element("status_code")?.Value ?? "200";
127 if (statusCode != "200")
129 var statusText = xElm.Element("status_txt")?.Value;
130 throw new WebApiException(statusText ?? $"status_code = {statusCode}");
133 var accessToken = xElm.Element("access_token")?.Value;
134 if (accessToken == null)
135 throw new WebApiException("Property `access_token` required");
141 private IEnumerable<KeyValuePair<string, string>> CreateAccessTokenParams()
143 if (string.IsNullOrEmpty(this.EndUserAccessToken))
147 new KeyValuePair<string, string>("login", this.EndUserLoginName),
148 new KeyValuePair<string, string>("apiKey", this.EndUserApiKey),
154 new KeyValuePair<string, string>("access_token", this.EndUserAccessToken),