• Hi,

    To all the good people here, please see if you can help me in this problem of mine. I am using WP-API and oAuth Server plugin to set up a REST connection from a third party C# application. Below is the code.
    While it works perfectly on a blog hosted in a free hosting (16mb.com), it chokes when I start using my own server.
    During POST it returns a 403 forbidden error. User not allowed to post. I did some changes to htaccess file as read in different blogs, but somehow it dint work. I tried changing wordpress folder permissions and a few tweaks in the code as well.
    I think its a server issue, but can anyone help me identify the root cause and any possible workaround.

    Below is the code I am using:

    var request = (HttpWebRequest)WebRequest.Create(“mysite/wp-json/wp/v2/posts”);
    request.Headers = new WebHeaderCollection();
    request.Headers[“Authorization”] = “Bearer ” + list.Token.getToken;
    request.Method = “POST”;

    string Title = getTitle(file);
    string Content = getContent(file);
    DateTime datePost = (When.Items[When.SelectedIndex] as ComboBoxItem).Name == “Now” ? DateTime.Now : dt.Value;
    var postData = “title=” + Title +
    “&content=” + Content +
    “&status=publish” +
    “&date=” + TimeZone.CurrentTimeZone.ToUniversalTime(datePost).ToString(“yyyy-MM-ddTHH:mm:ss”) +
    getSEO(Title,Content);

    var data = Encoding.ASCII.GetBytes(postData);
    request.ContentType = “application/x-www-form-urlencoded”;
    request.ContentLength = data.Length;
    using (var stream = request.GetRequestStream())
    stream.Write(data, 0, data.Length);
    var response = (HttpWebResponse)request.GetResponse();
    var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
    var json = JObject.Parse(“{” + JObject.Parse(responseString)[“guid”].First + “}”); // get link of post
    MessageBox.Show(“Post posted at “+json[“rendered”].ToString());

    https://wordpress.org/plugins/oauth2-provider/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter roy1

    (@roy1)

    This is the exact error I am facing:
    The remote server returned an error: (403) Forbidden.

    Plugin Author Justin Greer

    (@justingreerbbi)

    Hi @roy1,

    I am a bit confused. What grant type are you using to make the connection? If you are using basic auth code as the grant type, ensure that you are only using one form of authentication. This means, that if you are using an access token to authorize through the REST API then please ensure you are logged out of WP in your browser.

    Thread Starter roy1

    (@roy1)

    Hi Justin,

    Thanks a lot for replying back. Speaks volumes about the good help that you bring back to the wp community!

    Coming back to your answer : “I use OAuth, having a script that generate the token at mysite/script.php (which require authentication to work). This script use the plugin : WP OAuth Server “. The authentication and token exchange do work, and I am not logged in from any browser when the application is interacting.

    The problem comes up during the POST request. Is it something at my server end, because it works like a charm when using the same in my free webhost at 16mb.com

    Plugin Author Justin Greer

    (@justingreerbbi)

    Hmmm. If you are using any permalinks other than default, you may have to add a trailing slash to the server call.

    This happens because WP does a redirect if the domain does not have a trailing slash at the end of the domain. During the redirect the POST is lost and the server will reply with a 403 request must be POST (or something simular)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘403 forbidden, unable to do POST in blog hosted in my server’ is closed to new replies.