#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (C) 2007-2014 ShareX Developers
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 .
*/
#endregion License Information (GPL v3)
// Credits: https://github.com/l0nley
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using UploadersLib.HelperClasses;
namespace UploadersLib.FileUploaders
{
public sealed class OneDrive : FileUploader, IOAuth2
{
public OAuth2Info AuthInfo { get; private set; }
public string FolderId { get; set; }
public OneDrive(OAuth2Info authInfo)
{
AuthInfo = authInfo;
}
public override UploadResult Upload(Stream stream, string fileName)
{
if (!CheckAuthorization())
{
return null;
}
if (string.IsNullOrEmpty(FolderId))
{
FolderId = "me/skydrive/files";
}
var uploadUri = string.Format("https://apis.live.net/v5.0/{0}/{1}?access_token={2}&overwrite=true", FolderId, fileName, AuthInfo.Token.access_token);
var result = UploadData(stream, uploadUri, string.Empty, string.Empty, null, null, null, ResponseType.Text, HttpMethod.PUT);
if (!result.IsSuccess)
{
return result;
}
var resultJson = JsonConvert.DeserializeObject(result.Response);
result.URL = resultJson.source;
return result;
}
public string GetAuthorizationURL()
{
Dictionary args = new Dictionary();
args.Add("response_type", "code");
args.Add("client_id", AuthInfo.Client_ID);
args.Add("scope", "wl.skydrive_update");
return CreateQuery("https://login.live.com/oauth20_authorize.srf", args);
}
public bool GetAccessToken(string code)
{
Dictionary args = new Dictionary();
args.Add("grant_type", "authorization_code");
args.Add("code", code);
args.Add("client_id", AuthInfo.Client_ID);
args.Add("client_secret", AuthInfo.Client_Secret);
var response = SendRequest(HttpMethod.POST, "https://login.live.com/oauth20_token.srf", args);
if (string.IsNullOrEmpty(response))
{
return false;
}
OAuth2Token token = JsonConvert.DeserializeObject(response);
if (token == null || string.IsNullOrEmpty(token.access_token))
{
return false;
}
token.UpdateExpireDate();
AuthInfo.Token = token;
return true;
}
public bool RefreshAccessToken()
{
if (!OAuth2Info.CheckOAuth(AuthInfo) || string.IsNullOrEmpty(AuthInfo.Token.refresh_token))
{
return false;
}
Dictionary args = new Dictionary();
args.Add("grant_type", "refresh_token");
args.Add("refresh_token", AuthInfo.Token.refresh_token);
args.Add("client_id", AuthInfo.Client_ID);
args.Add("client_secret", AuthInfo.Client_Secret);
var response = SendRequest(HttpMethod.POST, "https://login.live.com/oauth20_token.srf", args);
if (string.IsNullOrEmpty(response))
{
return false;
}
OAuth2Token token = JsonConvert.DeserializeObject(response);
if (token == null || string.IsNullOrEmpty(token.access_token))
{
return false;
}
token.UpdateExpireDate();
AuthInfo.Token = token;
return true;
}
public bool CheckAuthorization()
{
if (OAuth2Info.CheckOAuth(AuthInfo))
{
if (!AuthInfo.Token.IsExpired || RefreshAccessToken())
{
return true;
}
Errors.Add("Refresh access token failed.");
return false;
}
Errors.Add("Live Id login is required.");
return false;
}
}
internal sealed class OneDriveUploadInfo
{
public string id { get; set; }
public string source { get; set; }
}
}