You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to post JSON to WordPress via the new WP-API, using the WP-API-OAUTH plugin.
Also, I'm using .Net 4.6
I have the access token, consumer key, and associated secrets; and I have used them successfully via Java tools.
However, using this tool in my .Net solution, I get a 401.
I am posting JSON as a raw string converted to Bytes.
My code is below. Any ideas what is wrong?
using OAuth;
using OAuth.Base;
using OAuth.Authenticator;
...
...
...
internal void post(string endpoint, string json)
{
try
{
AccessToken accessToken = new AccessToken("AcceSSToKen", "acCEssToKENsecREt");
ClientCredentials credentials = new ClientCredentials("CoNSuMeRKEy", "cONSuMERsECreT");
RequestAuthenticator authenticator = RequestAuthenticatorFactory.GetHmacSha1Authenticator(credentials, accessToken);
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create(endpoint);
authenticator.SignRequest(request);
//Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(json);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/json";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
The text was updated successfully, but these errors were encountered:
Full disclosure: I haven't touched that code in a very long time. So my memory is a little fuzzy.
Looking at your code I notice that you sign the request before filling in the method and headers. The signature depends on those fields so this is probably why you end up with an invalid request. It's as if someone tampered with the request on the wire. I believe that if you move the call to SignRequest before calling GetResponse it will work.
I am trying to post JSON to WordPress via the new WP-API, using the WP-API-OAUTH plugin.
Also, I'm using .Net 4.6
I have the access token, consumer key, and associated secrets; and I have used them successfully via Java tools.
However, using this tool in my .Net solution, I get a 401.
I am posting JSON as a raw string converted to Bytes.
My code is below. Any ideas what is wrong?
The text was updated successfully, but these errors were encountered: