ShareX/ShareX.UploadersLib/URLShorteners/BitlyURLShortener.cs

159 lines
5.5 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2020-02-05 20:19:48 +13:00
Copyright (c) 2007-2020 ShareX Team
2013-11-03 23:53:49 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
2013-12-21 04:59:26 +13:00
using Newtonsoft.Json;
2014-12-11 12:19:28 +13:00
using ShareX.HelpersLib;
2016-06-28 05:20:55 +12:00
using ShareX.UploadersLib.Properties;
2020-02-15 14:03:58 +13:00
using System;
2013-11-03 23:53:49 +13:00
using System.Collections.Generic;
2020-02-15 14:03:58 +13:00
using System.Collections.Specialized;
2016-06-28 05:20:55 +12:00
using System.Drawing;
2013-12-21 04:59:26 +13:00
using System.Web;
using System.Windows.Forms;
2013-11-03 23:53:49 +13:00
2014-12-11 09:25:20 +13:00
namespace ShareX.UploadersLib.URLShorteners
2013-11-03 23:53:49 +13:00
{
public class BitlyURLShortenerService : URLShortenerService
{
public override UrlShortenerType EnumValue { get; } = UrlShortenerType.BITLY;
2016-06-28 05:20:55 +12:00
public override Icon ServiceIcon => Resources.Bitly;
public override bool CheckConfig(UploadersConfig config)
2016-03-22 23:45:46 +13:00
{
return OAuth2Info.CheckOAuth(config.BitlyOAuth2Info);
2016-03-22 23:45:46 +13:00
}
public override URLShortener CreateShortener(UploadersConfig config, TaskReferenceHelper taskInfo)
{
if (config.BitlyOAuth2Info == null)
{
config.BitlyOAuth2Info = new OAuth2Info(APIKeys.BitlyClientID, APIKeys.BitlyClientSecret);
}
return new BitlyURLShortener(config.BitlyOAuth2Info)
{
Domain = config.BitlyDomain
};
}
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpBitly;
}
public sealed class BitlyURLShortener : URLShortener, IOAuth2Basic
2013-11-03 23:53:49 +13:00
{
2013-12-21 04:59:26 +13:00
private const string URLAPI = "https://api-ssl.bitly.com/";
private const string URLAccessToken = URLAPI + "oauth/access_token";
2020-02-15 14:03:58 +13:00
private const string URLShorten = URLAPI + "v4/shorten";
2013-11-03 23:53:49 +13:00
2013-12-21 07:20:58 +13:00
public OAuth2Info AuthInfo { get; private set; }
2014-06-18 08:10:31 +12:00
public string Domain { get; set; }
2013-11-03 23:53:49 +13:00
2013-12-21 04:59:26 +13:00
public BitlyURLShortener(OAuth2Info oauth)
{
AuthInfo = oauth;
}
2013-11-03 23:53:49 +13:00
2013-12-21 04:59:26 +13:00
public string GetAuthorizationURL()
2013-11-03 23:53:49 +13:00
{
2013-12-21 04:59:26 +13:00
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("client_id", AuthInfo.Client_ID);
args.Add("redirect_uri", Links.URL_CALLBACK);
return URLHelpers.CreateQueryString("https://bitly.com/oauth/authorize", args);
2013-12-21 04:59:26 +13:00
}
public bool GetAccessToken(string code)
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("client_id", AuthInfo.Client_ID);
args.Add("client_secret", AuthInfo.Client_Secret);
args.Add("code", code);
args.Add("redirect_uri", Links.URL_CALLBACK);
string response = SendRequestMultiPart(URLAccessToken, args);
2013-12-21 04:59:26 +13:00
if (!string.IsNullOrEmpty(response))
{
string token = HttpUtility.ParseQueryString(response)["access_token"];
if (!string.IsNullOrEmpty(token))
{
AuthInfo.Token = new OAuth2Token { access_token = token };
return true;
}
}
return false;
2013-11-03 23:53:49 +13:00
}
2020-02-15 14:03:58 +13:00
private NameValueCollection GetAuthHeaders()
{
NameValueCollection headers = new NameValueCollection();
headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token);
return headers;
}
2013-11-03 23:53:49 +13:00
public override UploadResult ShortenURL(string url)
{
UploadResult result = new UploadResult { URL = url };
if (!string.IsNullOrEmpty(url))
{
2020-02-15 14:03:58 +13:00
BitlyShortenRequestBody requestBody = new BitlyShortenRequestBody();
requestBody.long_url = url;
if (!string.IsNullOrEmpty(Domain)) requestBody.domain = Domain;
string json = JsonConvert.SerializeObject(requestBody);
2013-11-03 23:53:49 +13:00
2020-02-15 14:03:58 +13:00
NameValueCollection headers = GetAuthHeaders();
2013-11-03 23:53:49 +13:00
2020-02-15 14:03:58 +13:00
result.Response = SendRequest(HttpMethod.POST, URLShorten, json, RequestHelpers.ContentTypeJSON, null, headers);
2013-12-21 04:59:26 +13:00
2020-02-15 14:03:58 +13:00
BitlyShortenResponse responseData = JsonConvert.DeserializeObject<BitlyShortenResponse>(result.Response);
if (responseData != null && !string.IsNullOrEmpty(responseData.link))
2013-12-21 04:59:26 +13:00
{
2020-02-15 14:03:58 +13:00
result.ShortenedURL = responseData.link;
2013-12-21 04:59:26 +13:00
}
2013-11-03 23:53:49 +13:00
}
return result;
}
2013-12-21 04:59:26 +13:00
2020-02-15 14:03:58 +13:00
private class BitlyShortenRequestBody
2013-12-21 04:59:26 +13:00
{
public string long_url { get; set; }
2020-02-15 14:03:58 +13:00
public string domain { get; set; } = "bit.ly";
2013-12-21 04:59:26 +13:00
}
2020-02-15 14:03:58 +13:00
private class BitlyShortenResponse
2013-12-21 04:59:26 +13:00
{
2020-02-15 14:03:58 +13:00
public DateTime created_at { get; set; }
public string id { get; set; }
public string link { get; set; }
public string long_url { get; set; }
2013-12-21 04:59:26 +13:00
}
2013-11-03 23:53:49 +13:00
}
}