Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions RestAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
Expand Down Expand Up @@ -43,7 +41,7 @@ public class RestAPI
/// <summary>
/// Authenticate Woocommerce API with JWT when set to True
/// </summary>
public bool WCAuthWithJWT { get; set; }
internal bool WCAuthWithJWT { get; set; }

/// <summary>
/// Provide a function to modify the json string before deserilizing, this is for JWT Token ONLY!
Expand Down Expand Up @@ -79,11 +77,13 @@ public RestAPI(string url, string key, string secret, bool authorizedHeader = tr
Func<string, string> jsonSerializeFilter = null,
Func<string, string> jsonDeserializeFilter = null,
Action<HttpWebRequest> requestFilter = null,
Action<HttpWebResponse> responseFilter = null)//, bool useProxy = false)
Action<HttpWebResponse> responseFilter = null,
bool wcAuthWithJWT = false)//, bool useProxy = false)
{
if (string.IsNullOrEmpty(url))
throw new Exception("Please use a valid WooCommerce Restful API url.");

WCAuthWithJWT = wcAuthWithJWT;
string urlLower = url.Trim().ToLower().TrimEnd('/');
if (urlLower.EndsWith("wc-api/v1") || urlLower.EndsWith("wc-api/v2") || urlLower.EndsWith("wc-api/v3"))
Version = APIVersion.Legacy;
Expand Down Expand Up @@ -113,7 +113,7 @@ public RestAPI(string url, string key, string secret, bool authorizedHeader = tr
AuthorizedHeader = authorizedHeader;

//Why extra '&'? look here: https://wordpress.org/support/topic/woocommerce-rest-api-v3-problem-woocommerce_api_authentication_error/
if ((url.ToLower().Contains("wc-api/v3") || !IsLegacy) && !wc_url.StartsWith("https", StringComparison.OrdinalIgnoreCase) && !(Version == APIVersion.WordPressAPI || Version == APIVersion.WordPressAPIJWT))
if ((url.ToLower().Contains("wc-api/v3") || !IsLegacy) && !wc_url.StartsWith("https", StringComparison.OrdinalIgnoreCase) && !(Version == APIVersion.WordPressAPI || Version == APIVersion.WordPressAPIJWT) && (!WCAuthWithJWT))
wc_secret = secret + "&";
else
wc_secret = secret;
Expand Down Expand Up @@ -213,7 +213,7 @@ public virtual async Task<string> SendHttpClientRequest<T>(string endpoint, Requ
else
{
httpWebRequest = (HttpWebRequest)WebRequest.Create(wc_url + GetOAuthEndPoint(method.ToString(), endpoint, parms));
if (Version == APIVersion.WordPressAPIJWT)
if (JWT_Object != null)
httpWebRequest.Headers["Authorization"] = "Bearer " + JWT_Object.token;
}

Expand Down Expand Up @@ -336,7 +336,7 @@ protected string GetOAuthEndPoint(string method, string endpoint, Dictionary<str
return endpoint + "?" + requestParms.TrimEnd('&');
}
}

Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("oauth_consumer_key", wc_key);

Expand Down Expand Up @@ -364,7 +364,7 @@ protected string GetOAuthEndPoint(string method, string endpoint, Dictionary<str
dic.Add("oauth_signature", Common.GetSHA256(wc_secret + "&" + oauth_token_secret, base_request_uri));
else
dic.Add("oauth_signature", Common.GetSHA256(wc_secret, base_request_uri));

string parmstr = string.Empty;
foreach (var parm in dic)
parmstr += parm.Key + "=" + Uri.EscapeDataString(parm.Value) + "&";
Expand Down