2017-09-26 1 views
0

Le problème est que j'obtiens la réponse "Le serveur distant a renvoyé une erreur: (401) Non autorisé." im pas sûr de ce que je dois faire pour venir sur ce problème i im pensant qu'il doit être la façon dont la signature est générée peut-être crachant une signature non validevb.net woocommerce api authentification

sorties Wireshark ce à partir du code

POST /wp-json/wc/v2/products HTTP/1.1 
Authorization: OAuth oauth_consumer_key="key",oauth_signature_method="HMAC-SHA256",oauth_timestamp="1506468745",oauth_nonce="NjM2NDIwNTExNDUxOTkyMjEy",oauth_version="1.0",oauth_signature="sig" 
Content-Type: application/json 
Host: example.com 
Content-Length: 890 
Connection: Keep-Alive 

{ 
    "name": "Premium Quality", 
    "type": "simple", 
    "regular_price": "21.99", 
    "description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.", 
    "short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.", 
    "categories": [ 
    { 
     "id": 9 
    }, 
    { 
     "id": 14 
    } 
    ], 
    "images": [ 
    { 
     "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg", 
     "position": 0 
    }, 
    { 
     "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg", 
     "position": 1 
    } 
    ] 
}HTTP/1.1 401 Unauthorized 
Date: Tue, 26 Sep 2017 23:32:24 GMT 
Server: Apache 
X-Powered-By: PHP/5.5.38 
X-Robots-Tag: noindex 
Link: <http://example.com/wp-json/>; rel="https://api.w.org/" 
X-Content-Type-Options: nosniff 
Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages 
Access-Control-Allow-Headers: Authorization, Content-Type 
Vary: Accept-Encoding,User-Agent 
Keep-Alive: timeout=5 
Connection: Keep-Alive 
Transfer-Encoding: chunked 
Content-Type: application/json; charset=UTF-8 

89 
{"code":"woocommerce_rest_authentication_error","message":"Invalid signature - provided signature does not match.","data":{"status":401}} 
0 

lors de l'utilisation postman i obtenir une réponse qui fonctionne et crée le produit sans problème

POST /wp-json/wc/v2/products HTTP/1.1 
Host: example.com 
Connection: keep-alive 
Content-Length: 890 
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36 
Cache-Control: no-cache 
Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop 
Content-Type: application/json 
Authorization: OAuth oauth_consumer_key="key",oauth_signature_method="HMAC-SHA256",oauth_timestamp="1506468730",oauth_nonce="HJVzFL",oauth_version="1.0",oauth_signature="sig" 
Postman-Token: 95240347-868c-03d0-55a9-d2fb7c51b8e2 
Accept: */* 
DNT: 1 
Accept-Encoding: gzip, deflate 
Accept-Language: en-US,en;q=0.8 
Cookie: woocommerce_items_in_cart=1; woocommerce_cart_hash=some hash; wp_woocommerce_session_d0d4322f0d3b54c7984c1c37276328=somenumbers 

{ 
    "name": "Premium Quality", 
    "type": "simple", 
    "regular_price": "21.99", 
    "description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.", 
    "short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.", 
    "categories": [ 
    { 
     "id": 9 
    }, 
    { 
     "id": 14 
    } 
    ], 
    "images": [ 
    { 
     "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg", 
     "position": 0 
    }, 
    { 
     "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg", 
     "position": 1 
    } 
    ] 
}HTTP/1.1 201 Created 
Date: Tue, 26 Sep 2017 23:32:09 GMT 
Server: Apache 
X-Powered-By: PHP/5.5.38 
X-Robots-Tag: noindex 
Link: <http://example.com/wp-json/>; rel="https://api.w.org/" 
X-Content-Type-Options: nosniff 
Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages 
Access-Control-Allow-Headers: Authorization, Content-Type 
Expires: Wed, 11 Jan 1984 05:00:00 GMT 
Cache-Control: no-cache, must-revalidate, max-age=0 
Allow: GET, POST 
Access-Control-Allow-Origin: 
Access-Control-Allow-Methods: OPTIONS, GET, POST, PUT, PATCH, DELETE 
Access-Control-Allow-Credentials: true 
Vary: Origin,Accept-Encoding,User-Agent 
Set-Cookie: woocommerce_items_in_cart=1; path=/ 
Set-Cookie: woocommerce_cart_hash=some hash; path=/ 
Set-Cookie: wp_woocommerce_session_d0d4322f0d3b545798c1cb7276328=some hash; expires=Thu, 28-Sep-2017 16:25:49 GMT; Max-Age=147217; path=/ 
Location: http://example.com/wp-json/wc/v2/products/1066 
Content-Encoding: gzip 
Content-Length: 1041 
Keep-Alive: timeout=5 
Connection: Keep-Alive 
Content-Type: application/json; charset=UTF-8 

...........V.n#7...a 
........ 
P...Rt..u1.5.X.....#...hn..i.....X").:.!...u.^.WW..pM.....e.......bR...(\...u..%T.....B....7..<D.i.....IG!/)... .7s.m.D..g.7s..y.Jx..r+....i....>.V...z.|fT5:>5\^<5...;.....U...2JAj.(..yL!...J.=D;d.|...*...<re.. .....u.nZ.5.......?...R.L.p....VFn"..o%.^.(.".!..."3./.....9..9...!.'.l.......r.........z.v...#; .MXR.%$.'..E...AF... '...,.`...[.v..N...U.Tf.W....o-.x.v3.;O..s...H......)...(.......w...v..6!^.~.!..r1[....$.}.\..|...fM..w..bm.R/(.J|A.....O.!.}.*..........8Z.*j...7.l..M&?]....o...=.q....V....]\..........W.......{.x&.:..Q. Ik.6....G...Hm.FY<.... ..O...VJj ...3..;.O...#y.x...Q.)Fd......W#....Y..E......`[email protected][.w.!.`.]x....5.....\[email protected]&.$..P. M.w.....~<...%....<.%.F.2......g.<.Uy...".I...u...t...<..A9+...v_.\..y.v|9.xr......".S....8....U.......X/.?.y...FZy.0...^.P...(.;..7....V...^|.0>...h. .<..K.y.......c.........g...w.V\.5.,.~........;.Z............=T...Dj...... .g...v...z.?p....8..o....O.^ 
.....m.c 
. 
h.N[.... .......v_...` 
.b3?,.O9...!.|%a1.Ew.o.........[.. 
.. 

ive essayé quelques choses diffrent qui ont et imporoved le code certains cependant je ne peux pas encore semble ge t la signature de générer correctement son toujours en disant qu'il ne marche pas correspondre

Imports RestSharp Imports System.Text 
Imports System.Security.Cryptography 

Public Class Form1 

    Shared oauth_consumer_key As String = "ck_xxx" 
    Shared oauth_consumer_Secret As String = "cs_XXX" 


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     Dim resource_url As String = "http://example.com/wp-json/wc/v2/products" 

     Dim oauth_signature_method As String = "HMAC-SHA256" 
     Dim timeSpan As TimeSpan = DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc) 
     Dim oauth_timestamp As String = Convert.ToInt64(timeSpan.TotalSeconds).ToString() 
     Dim oauth_nonce As String = Convert.ToBase64String(New ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString())) 
     Dim oauth_version As String = "1.0" 

     Dim oauth_signature As String = GeneraOAuthSignature(resource_url, oauth_nonce, oauth_signature_method, oauth_timestamp, oauth_version) 

     Dim auth As String = "oauth_consumer_key=""" & oauth_consumer_key & """,oauth_signature_method=""" & oauth_signature_method & """,oauth_timestamp=""" & oauth_timestamp & """,oauth_nonce=""" & oauth_nonce & """,oauth_version=""" & oauth_version & """,oauth_signature=""" & oauth_signature & """" 
     MsgBox(auth) 
     Dim client = New RestClient(resource_url) 
     Dim request = New RestRequest(Method.POST) 
     request.AddHeader("cache-control", "no-cache") 
     request.AddHeader("content-type", "application/json") 
     request.AddHeader("authorization", "OAuth " & auth) 
     request.AddParameter("application/json", TextBox1.Text, ParameterType.RequestBody) 
     Dim response As IRestResponse = client.Execute(request) 
     MsgBox(response.Content) 
    End Sub 

    Shared Function GeneraOAuthSignature(ByVal stream_url As String, ByVal oauth_nonce As String, ByVal oauth_signature_method As String, ByVal oauth_timestamp As String, ByVal oauth_version As String) As String 
     'The next step is to generate an encrypted oAuth signature which will be use to validate the request. 
     'To do this, all of the request data is concatenated into a particular format as follows 
     Dim baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}&oauth_timestamp={3}&oauth_version={4}" 

     Dim baseString As String = String.Format(baseFormat, oauth_consumer_key, oauth_nonce, oauth_signature_method, oauth_timestamp, oauth_version) 

     baseString = String.Concat("POST&", Uri.EscapeDataString(stream_url), "&", Uri.EscapeDataString(baseString)) 
     MsgBox(baseString) 
     Dim compositeKey As String = Uri.EscapeDataString(oauth_consumer_Secret) 

     'Using this base string, we then encrypt the data using a composite of the secret keys and the HMAC-SHA1 algorithm. 
     Dim oauth_signature As String 

     Dim hasher As HMACSHA256 = New HMACSHA256(ASCIIEncoding.ASCII.GetBytes(compositeKey)) 
     Using hasher 
      oauth_signature = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString))) 
     End Using 

     Return oauth_signature 
    End Function 

End Class 

Répondre

0

Vous ne pas besoin de tout ce code que vous avez écrit, il est plus facile que vous le pensez. Lisez la section Usage du dépôt github XiaoFaye/WooCommerce.NET.

, vous pouvez également installer le WooCommerceNet des nugets:
VS > Tools > NuGet Package Manager > Manage NuGet