I writing app for UWP(C#)
I need to connect to woo commerce on web site.
I write some code and has errors.
In Main .cs file I write this code
string ConsumerKey = "ck_f03bbd67e26a96604ddb188dbd63be3d252891ab";
string ConsumerSecret = "cs_f8583f42dd1d75da832574b7ad6e649a0687f88f";
string StoreUrl = "https://www.simplegames.com.ua";
bool Isssl = true;
WoocommerceApiClient client2 = new WoocommerceApiClient(ConsumerKey, ConsumerSecret, StoreUrl, Isssl);
string orders = client2.GetProducts();
with this code is all okay.
Also I have class with connecting
Code of class.
private static byte[] HashHMAC(byte[] key, byte[] message)
{
var hash = new HMACSHA256(key);
return hash.ComputeHash(message);
}
private string Hash(string input)
{
using (SHA1Managed sha1 = new SHA1Managed())
{
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
var sb = new StringBuilder(hash.Length * 2);
foreach (byte b in hash)
{
// can be "x2" if you want lowercase
sb.Append(b.ToString("X2"));
}
return sb.ToString();
}
}
public const string API_ENDPOINT = "wc-api/v1/";
public string ApiUrl { get; set; }
public string ConsumerSecret { get; set; }
public string ConsumerKey { get; set; }
public bool IsSsl { get; set; }
public WoocommerceApiClient(string consumerKey, string consumerSecret, string storeUrl, bool isSsl = false)
{
if (string.IsNullOrEmpty(consumerKey) || string.IsNullOrEmpty(consumerSecret) ||
string.IsNullOrEmpty(storeUrl))
{
throw new ArgumentException("ConsumerKey, consumerSecret and storeUrl are required");
}
this.ConsumerKey = consumerKey;
this.ConsumerSecret = consumerSecret;
this.ApiUrl = storeUrl.TrimEnd('/') + "/" + API_ENDPOINT;
this.IsSsl = isSsl;
}
public string GetAllProducts()
{
return MakeApiCall("products", new Dictionary<string, string>() { { "filter[limit]", "2000" } });
}
public string GetProducts()
{
return MakeApiCall("products");
}
private string MakeApiCall(string endpoint, Dictionary<string, string> parameters = null, string method = "GET")
{
if (parameters == null)
{
parameters = new Dictionary<string, string>();
}
parameters["oauth_consumer_key"] = this.ConsumerKey;
parameters["oauth_timestamp"] =
DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds.ToString();
parameters["oauth_timestamp"] = parameters["oauth_timestamp"].Substring(0,
parameters["oauth_timestamp"].IndexOf("."));
parameters["oauth_nonce"] = Hash(parameters["oauth_timestamp"]);
parameters["oauth_signature_method"] = "HMAC-SHA256";
parameters["oauth_signature"] = GenerateSignature(parameters, method, endpoint);
WebClient wc = new WebClient();
StringBuilder sb = new StringBuilder();
foreach (var pair in parameters)
{
sb.AppendFormat("&{0}={1}", HttpUtility.UrlEncode(pair.Key), HttpUtility.UrlEncode(pair.Value));
}
var url = this.ApiUrl + endpoint + "?" + sb.ToString().Substring(1).Replace("%5b", "%5B").Replace("%5d", "%5D");
var result = wc.DownloadString(url);
return result;
}
private string GenerateSignature(Dictionary<string, string> parameters, string method, string endpoint)
{
var baserequesturi = Regex.Replace(HttpUtility.UrlEncode(this.ApiUrl + endpoint), "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper());
var normalized = NormalizeParameters(parameters);
var signingstring = string.Format("{0}&{1}&{2}", method, baserequesturi,
string.Join("%26", normalized.OrderBy(x => x.Key).ToList().ConvertAll(x => x.Key + "%3D" + x.Value)));
var signature =
Convert.ToBase64String(HashHMAC(Encoding.UTF8.GetBytes(this.ConsumerSecret),
Encoding.UTF8.GetBytes(signingstring)));
Debug.WriteLine(signature);
return signature;
}
private Dictionary<string, string> NormalizeParameters(Dictionary<string, string> parameters)
{
var result = new Dictionary<string, string>();
foreach (var pair in parameters)
{
var key = HttpUtility.UrlEncode(HttpUtility.UrlDecode(pair.Key));
key = Regex.Replace(key, "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper()).Replace("%", "%25");
var value = HttpUtility.UrlEncode(HttpUtility.UrlDecode(pair.Value));
value = Regex.Replace(value, "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper()).Replace("%", "%25");
result.Add(key, value);
}
return result;
}
I have this errors.
1)
Severity Code Description Project File Line Suppression State
Error CS0103 The name 'HttpUtility' does not exist in the current context Milano C:\Users\nemes\Documents\GitHub\Milano_pizza\Milano\WoocommerceApiClient.cs 85 Active
2) Severity Code Description Project File Line Suppression State Error CS1061 'List<KeyValuePair<string, string>>' does not contain a definition for 'ConvertAll' and no extension method 'ConvertAll' accepting a first argument of type 'List<KeyValuePair<string, string>>' could be found (are you missing a using directive or an assembly reference?) Milano C:\\Users\\nemes\\Documents\\GitHub\\Milano_pizza\\Milano\\WoocommerceApiClient.cs 98 Active
3) Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name 'HMACSHA256' could not be found (are you missing a using directive or an assembly reference?) Milano C:\Users\nemes\Documents\GitHub\Milano_pizza\Milano\WoocommerceApiClient.cs 18 Active
4) Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name 'SHA1Managed' could not be found (are you missing a using directive or an assembly reference?) Milano C:\Users\nemes\Documents\GitHub\Milano_pizza\Milano\WoocommerceApiClient.cs 24 Active
5)Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name 'WebClient' could not be found (are you missing a using directive or an assembly reference?) Milano C:\Users\nemes\Documents\GitHub\Milano_pizza\Milano\WoocommerceApiClient.cs 81 Active
6)Severity Code Description Project File Line Suppression State
Error CS1579 foreach statement cannot operate on variables of type '?' because '?' does not contain a public definition for 'GetEnumerator' Milano C:\Users\nemes\Documents\GitHub\Milano_pizza\Milano\WoocommerceApiClient.cs 29 Active
Help me fix this errors please.
Thank's for your help.
UPDATE
I think I found fix for this error
1)
Severity Code Description Project File Line Suppression State
Error CS0103 The name 'HttpUtility' does not exist in the current context Milano C:\Users\nemes\Documents\GitHub\Milano_pizza\Milano\WoocommerceApiClient.cs 85 Active
It is System.Net.WebUtility.HtmlEncode
Is this right?
The .Net cryptography API is no longer available. It has been replaced by the WinRT api available from Windows.Security.Cryptography.Core
To generate the hash code you need, you will have to use CryptographicHash class
// Create a string that contains the name of the hashing algorithm to use.
String strAlgName = HashAlgorithmNames.Sha512;
// Create a HashAlgorithmProvider object.
HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(strAlgName);
// Create a CryptographicHash object. This object can be reused to continually
// hash new messages.
CryptographicHash objHash = objAlgProv.CreateHash();
// Hash message 1.
String strMsg1 = "This is message 1.";
IBuffer buffMsg1 = CryptographicBuffer.ConvertStringToBinary(strMsg1, BinaryStringEncoding.Utf16BE);
objHash.Append(buffMsg1);
IBuffer buffHash1 = objHash.GetValueAndReset();
Using the CryptographicBuffer , you can then easily transform the hash's buffer to a base64 or hex string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.