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
|
2015-08-13 13:07:38 +12:00
|
|
|
|
Copyright (c) 2007-2015 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;
|
|
|
|
|
using ShareX.UploadersLib.HelperClasses;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System.Collections.Generic;
|
2013-12-21 04:59:26 +13:00
|
|
|
|
using System.Web;
|
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
|
|
|
|
{
|
2014-06-09 07:11:44 +12:00
|
|
|
|
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";
|
|
|
|
|
private const string URLShorten = URLAPI + "v3/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 CreateQuery("https://bitly.com/oauth/authorize", args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2014-04-17 05:55:07 +12:00
|
|
|
|
string response = SendRequest(HttpMethod.POST, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override UploadResult ShortenURL(string url)
|
|
|
|
|
{
|
|
|
|
|
UploadResult result = new UploadResult { URL = url };
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(url))
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, string> arguments = new Dictionary<string, string>();
|
2013-12-21 04:59:26 +13:00
|
|
|
|
arguments.Add("access_token", AuthInfo.Token.access_token);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
arguments.Add("longUrl", url);
|
2014-06-18 08:10:31 +12:00
|
|
|
|
if (!string.IsNullOrEmpty(Domain)) arguments.Add("domain", Domain);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-04-17 05:55:07 +12:00
|
|
|
|
result.Response = SendRequest(HttpMethod.GET, URLShorten, arguments);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2013-12-21 07:20:58 +13:00
|
|
|
|
BitlyShortenResponse shorten = JsonConvert.DeserializeObject<BitlyShortenResponse>(result.Response);
|
2013-12-21 04:59:26 +13:00
|
|
|
|
|
|
|
|
|
if (shorten != null && shorten.data != null && !string.IsNullOrEmpty(shorten.data.url))
|
|
|
|
|
{
|
|
|
|
|
result.ShortenedURL = shorten.data.url;
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2013-12-21 04:59:26 +13:00
|
|
|
|
|
|
|
|
|
public class BitlyShortenData
|
|
|
|
|
{
|
|
|
|
|
public string global_hash { get; set; }
|
|
|
|
|
public string hash { get; set; }
|
|
|
|
|
public string long_url { get; set; }
|
|
|
|
|
public int new_hash { get; set; }
|
|
|
|
|
public string url { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-21 07:20:58 +13:00
|
|
|
|
public class BitlyShortenResponse
|
2013-12-21 04:59:26 +13:00
|
|
|
|
{
|
|
|
|
|
public BitlyShortenData data { get; set; }
|
|
|
|
|
public int status_code { get; set; }
|
|
|
|
|
public string status_txt { get; set; }
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|