Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not authorized, despite correct key and secrets #1

Open
mjacobsen4DFM opened this issue Feb 3, 2016 · 1 comment
Open

Not authorized, despite correct key and secrets #1

mjacobsen4DFM opened this issue Feb 3, 2016 · 1 comment

Comments

@mjacobsen4DFM
Copy link

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());
            }
        }
@MathieuTurcotte
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants