Giter Site home page Giter Site logo

csharp-oauth-client-library's People

Contributors

mathieuturcotte avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

skela soleymoroz

csharp-oauth-client-library's Issues

Not authorized, despite correct key and secrets

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());
            }
        }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.