Flickr OAuth v1 upload works now

This commit is contained in:
Jaex 2017-10-06 01:15:47 +03:00
parent 1219ef78b7
commit 688ea9d603
2 changed files with 38 additions and 186 deletions

View file

@ -53,6 +53,11 @@ public static class OAuthManager
private const string RSASHA1SignatureType = "RSA-SHA1";
public static string GenerateQuery(string url, Dictionary<string, string> args, HttpMethod httpMethod, OAuthInfo oauth)
{
return GenerateQuery(url, args, httpMethod, oauth, out _);
}
public static string GenerateQuery(string url, Dictionary<string, string> args, HttpMethod httpMethod, OAuthInfo oauth, out Dictionary<string, string> parameters)
{
if (string.IsNullOrEmpty(oauth.ConsumerKey)
|| (string.IsNullOrEmpty(oauth.ConsumerSecret) && oauth.SignatureMethod == OAuthInfo.OAuthInfoSignatureMethod.HMAC_SHA1)
@ -61,7 +66,7 @@ public static string GenerateQuery(string url, Dictionary<string, string> args,
throw new Exception("ConsumerKey or ConsumerSecret or ConsumerPrivateKey empty.");
}
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters = new Dictionary<string, string>();
parameters.Add(ParameterVersion, oauth.OAuthVersion);
parameters.Add(ParameterNonce, GenerateNonce());
parameters.Add(ParameterTimestamp, GenerateTimestamp());
@ -122,8 +127,10 @@ public static string GenerateQuery(string url, Dictionary<string, string> args,
throw new NotImplementedException("Unsupported signature method");
}
string signature = URLHelpers.URLEncode(Convert.ToBase64String(signatureData));
return string.Format("{0}?{1}&{2}={3}", normalizedUrl, normalizedParameters, ParameterSignature, signature);
string signature = Convert.ToBase64String(signatureData);
parameters[ParameterSignature] = signature;
return string.Format("{0}?{1}&{2}={3}", normalizedUrl, normalizedParameters, ParameterSignature, URLHelpers.URLEncode(signature));
}
public static string GetAuthorizationURL(string requestTokenResponse, OAuthInfo oauth, string authorizeURL, string callback = null)

View file

@ -25,12 +25,10 @@ You should have received a copy of the GNU General Public License
using ShareX.HelpersLib;
using ShareX.UploadersLib.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
@ -57,34 +55,20 @@ public override GenericUploader CreateUploader(UploadersConfig config, TaskRefer
public class FlickrUploader : ImageUploader, IOAuth
{
private string API_Key, API_Secret;
private const string API_URL = "https://api.flickr.com/services/rest/";
private const string API_Auth_URL = "https://www.flickr.com/services/auth/";
public FlickrAuthInfo Auth = new FlickrAuthInfo();
public FlickrSettings Settings = new FlickrSettings();
public string Frob;
public OAuthInfo AuthInfo { get; set; }
public FlickrUploader(string key, string secret)
{
API_Key = key;
API_Secret = secret;
}
public FlickrUploader(string key, string secret, FlickrAuthInfo auth, FlickrSettings settings) : this(key, secret)
{
Auth = auth;
Settings = settings;
}
public FlickrSettings Settings { get; set; } = new FlickrSettings();
public FlickrUploader(OAuthInfo oauth)
{
AuthInfo = oauth;
}
public FlickrUploader(OAuthInfo oauth, FlickrSettings settings)
{
AuthInfo = oauth;
Settings = settings;
}
public string GetAuthorizationURL()
{
Dictionary<string, string> args = new Dictionary<string, string>();
@ -101,140 +85,39 @@ public bool GetAccessToken(string verificationCode = null)
return GetAccessToken("https://www.flickr.com/services/oauth/access_token", AuthInfo);
}
#region Auth
/// <summary>
/// Returns the credentials attached to an authentication token.
/// http://www.flickr.com/services/api/flickr.auth.checkToken.html
/// </summary>
public FlickrAuthInfo CheckToken(string token)
public override UploadResult Upload(Stream stream, string fileName)
{
string url = "https://up.flickr.com/services/upload/";
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("method", "flickr.auth.checkToken");
args.Add("api_key", API_Key);
args.Add("auth_token", token);
args.Add("api_sig", GetAPISig(args));
string response = SendRequestMultiPart(API_URL, args);
if (!string.IsNullOrEmpty(Settings.Title)) args.Add("title", Settings.Title);
if (!string.IsNullOrEmpty(Settings.Description)) args.Add("description", Settings.Description);
if (!string.IsNullOrEmpty(Settings.Tags)) args.Add("tags", Settings.Tags);
if (!string.IsNullOrEmpty(Settings.IsPublic)) args.Add("is_public", Settings.IsPublic);
if (!string.IsNullOrEmpty(Settings.IsFriend)) args.Add("is_friend", Settings.IsFriend);
if (!string.IsNullOrEmpty(Settings.IsFamily)) args.Add("is_family", Settings.IsFamily);
if (!string.IsNullOrEmpty(Settings.SafetyLevel)) args.Add("safety_level", Settings.SafetyLevel);
if (!string.IsNullOrEmpty(Settings.ContentType)) args.Add("content_type", Settings.ContentType);
if (!string.IsNullOrEmpty(Settings.Hidden)) args.Add("hidden", Settings.Hidden);
Auth = new FlickrAuthInfo(ParseResponse(response, "auth"));
string query = OAuthManager.GenerateQuery(url, args, HttpMethod.POST, AuthInfo, out Dictionary<string, string> parameters);
return Auth;
}
UploadResult result = SendRequestFile(url, stream, fileName, "photo", parameters);
/// <summary>
/// Returns a frob to be used during authentication.
/// http://www.flickr.com/services/api/flickr.auth.getFrob.html
/// </summary>
public string GetFrob()
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("method", "flickr.auth.getFrob");
args.Add("api_key", API_Key);
args.Add("api_sig", GetAPISig(args));
string response = SendRequestMultiPart(API_URL, args);
XElement eFrob = ParseResponse(response, "frob");
if (eFrob == null)
if (result.IsSuccess)
{
string errorMessage = ToErrorString();
XElement xele = ParseResponse(result.Response, "photoid");
if (string.IsNullOrEmpty(errorMessage))
if (null != xele)
{
throw new Exception("getFrob failed.");
string photoid = xele.Value;
//string url = URLHelpers.CombineURL(GetPhotosLink(), photoid);
//result.URL = URLHelpers.CombineURL(url, "sizes/o");
}
throw new Exception(errorMessage);
}
Frob = eFrob.Value;
return Frob;
}
/// <summary>
/// Get the full authentication token for a mini-token.
/// http://www.flickr.com/services/api/flickr.auth.getFullToken.html
/// </summary>
public FlickrAuthInfo GetFullToken(string frob)
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("method", "flickr.auth.getFullToken");
args.Add("api_key", API_Key);
args.Add("mini_token", frob);
args.Add("api_sig", GetAPISig(args));
string response = SendRequestMultiPart(API_URL, args);
Auth = new FlickrAuthInfo(ParseResponse(response, "auth"));
return Auth;
}
/// <summary>
/// Returns the auth token for the given frob, if one has been attached.
/// http://www.flickr.com/services/api/flickr.auth.getToken.html
/// </summary>
/// <returns></returns>
public FlickrAuthInfo GetToken(string frob)
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("method", "flickr.auth.getToken");
args.Add("api_key", API_Key);
args.Add("frob", frob);
args.Add("api_sig", GetAPISig(args));
string response = SendRequestMultiPart(API_URL, args);
Auth = new FlickrAuthInfo(ParseResponse(response, "auth"));
return Auth;
}
public FlickrAuthInfo GetToken()
{
return GetToken(Frob);
}
public string GetAuthLink()
{
return GetAuthLink(FlickrPermission.Write);
}
public string GetAuthLink(FlickrPermission perm)
{
if (!string.IsNullOrEmpty(Frob))
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("api_key", API_Key);
args.Add("perms", perm.ToString().ToLowerInvariant());
args.Add("frob", Frob);
args.Add("api_sig", GetAPISig(args));
return string.Format("{0}?{1}", API_Auth_URL, string.Join("&", args.Select(x => x.Key + "=" + x.Value).ToArray()));
}
return null;
}
public string GetPhotosLink(string userID)
{
return URLHelpers.CombineURL("https://www.flickr.com/photos", userID);
}
public string GetPhotosLink()
{
return GetPhotosLink(Auth.UserID);
}
#endregion Auth
#region Helpers
private string GetAPISig(Dictionary<string, string> args)
{
return TranslatorHelper.TextToHash(args.OrderBy(x => x.Key).Aggregate(API_Secret, (x, x2) => x + x2.Key + x2.Value), HashType.MD5);
return result;
}
private XElement ParseResponse(string response, string field)
@ -254,7 +137,6 @@ private XElement ParseResponse(string response, string field)
XElement err = xele.Element("err");
string code = err.GetAttributeValue("code");
string msg = err.GetAttributeValue("msg");
// throw new Exception(string.Format("Code: {0}, Message: {1}", code, msg));
Errors.Add(msg);
break;
}
@ -263,43 +145,6 @@ private XElement ParseResponse(string response, string field)
return null;
}
#endregion Helpers
public override UploadResult Upload(Stream stream, string fileName)
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("api_key", API_Key);
args.Add("auth_token", Auth.Token);
if (!string.IsNullOrEmpty(Settings.Title)) args.Add("title", Settings.Title);
if (!string.IsNullOrEmpty(Settings.Description)) args.Add("description", Settings.Description);
if (!string.IsNullOrEmpty(Settings.Tags)) args.Add("tags", Settings.Tags);
if (!string.IsNullOrEmpty(Settings.IsPublic)) args.Add("is_public", Settings.IsPublic);
if (!string.IsNullOrEmpty(Settings.IsFriend)) args.Add("is_friend", Settings.IsFriend);
if (!string.IsNullOrEmpty(Settings.IsFamily)) args.Add("is_family", Settings.IsFamily);
if (!string.IsNullOrEmpty(Settings.SafetyLevel)) args.Add("safety_level", Settings.SafetyLevel);
if (!string.IsNullOrEmpty(Settings.ContentType)) args.Add("content_type", Settings.ContentType);
if (!string.IsNullOrEmpty(Settings.Hidden)) args.Add("hidden", Settings.Hidden);
args.Add("api_sig", GetAPISig(args));
UploadResult result = SendRequestFile("https://up.flickr.com/services/upload/", stream, fileName, "photo", args);
if (result.IsSuccess)
{
XElement xele = ParseResponse(result.Response, "photoid");
if (null != xele)
{
string photoid = xele.Value;
string url = URLHelpers.CombineURL(GetPhotosLink(), photoid);
result.URL = URLHelpers.CombineURL(url, "sizes/o");
}
}
return result;
}
}
public class FlickrAuthInfo