• When I ran the following [program], an error occurred as described in [Execution result]. I would like to solve the error and realize article posting by executing the program, but is it possible to teach me how to deal with it?
    [WordPress version]: 6.2.1
    [Server]: Xfree
    [Program execution environment]: Jupyter Notebook
    [Programming language] python (Python 3.8.5)
    
    
    【program】
    **************************************************** ****************
    import requests
    import json
    
    AUTH_USER = 'YYYYYYY'
    AUTH_PASS = 'XXXX XXXX XXXX XXXX XXXX XXXX'
    
    END_POINT_URL = 'https://YYYYYYY.wp.xdomain.jp/wp-json/wp/v2/posts/'
    
    
    p_title = "Post"
    p_content = "Post"
    p_status = "publish"
    
    payload = {
                 'title': p_title ,
                 'content' : p_content ,
                 'status' : p_status,
                 'slug' : 'python_wp_rest_api_post'
                 }
    
    headers = {'content-type': "application/json"}
    
    r = requests.post( END_POINT_URL, data=json.dumps(payload) , headers=headers, auth=(AUTH_USER, AUTH_PASS) )
    print(r)
    **************************************************** ****************
    
    【Execution result】
    <Response [403]>
    
    
    [Confirmed items]
    ?Application password is already set
    ?Restrictions on access to “WordPress REST API” from foreign IP addresses have been disabled
    ?Confirmed that the AUTH_USER and AUTH_PASS values match the actual WordPress username and password
    ?Confirmed that the endpoint URL is correct
    ?Confirmed that I'm using the credentials of a user with WordPress administrator privileges
    
    
    
Viewing 9 replies - 1 through 9 (of 9 total)
  • I have created an Application Password to try to reproduce this problem.

    In my local environment:

    • Local site: https://glotpress.test
    • User: admin
    • Password: “S8WA XL45 1sqD vbpA I79H QvVk” (without quotes).

    First, I try to use the curl command to create a new post and it worked:

    curl --user "admin:S8WA XL45 1sqD vbpA I79H QvVk" -d '{"title": "Test title" , "content" : "Test content" , "status" : "publish", "slug" : "test-title"}' -H "Content-Type: application/json" -X POST https://glotpress.test/wp-json/wp/v2/posts/

    Then I translated this to Python 3, save this code in a test.py file and this is the working code:

    import requests
    
    url = 'https://glotpress.test/wp-json/wp/v2/posts/'
    headers = {'Content-Type': 'application/json'}
    auth = ('admin', 'S8WA XL45 1sqD vbpA I79H QvVk')
    data = {
        'title': 'Test title',
        'content': 'Test content',
        'status': 'publish',
        'slug': 'test-title'
    }
    
    response = requests.post(url, headers=headers, auth=auth, json=data)
    print(response.text)

    To execute it, you need to run:

    python3 test.py
    Thread Starter yasu7

    (@yasu7)

    @amieiro
    Thank you for your advice! The curl command was able to post the article successfully! However, the python program tried several times with my parameters and got error code 403. I have already confirmed the items listed in [Confirmed items] below, but if you have any other ideas about what to do to resolve the error, I am very sorry, but could you please let me know?

    [Confirmed items]
    ?Application password is already set
    ?Restrictions on access to “WordPress REST API” from foreign IP addresses have been disabled
    ?Confirmed that the endpoint URL is correct
    ?Confirmed that I’m using the credentials of a user with WordPress administrator privileges

    Hi @yasu7

    I was able to test both codes in my local machine and both worked.

    If the Bash command works in your machine, but the Python doesn’t work, I think it could be a problem related with your execution environment (if the URL and Auth parameters are fine). Can you paste here the full error you are getting in your terminal?

    Thread Starter yasu7

    (@yasu7)

    Hi @amieiro

    Thank you for your help.
    Here's what I see running in Jupyter Notebook(the whole error):
    
    
    ------------------------------------------------------------------------------------------------------------------------------
    <!DOCTYPE html>
    <html lang="ja">
    <head>
    <meta charset="EUC-JP" />
    <title>403 Forbidden</title>
    <meta name="copyright" content="Copyright XSERVER Inc.">
    <meta name="robots" content="INDEX,FOLLOW" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0">
    <style type="text/css">
    * {
        margin: 0;
        padding: 0;
    }
    img {
        border: 0;
    }
    ul {
        padding-left: 2em;
    }
    html {
        overflow-y: scroll;
        background: #3b79b7;
    }
    body {
        font-family: "????", Meiryo, "??? ?Х??????", "MS PGothic", "??饮?γ?? Pro W3", "Hiragino Kaku Gothic Pro", sans-serif;
        margin: 0;
        line-height: 1.4;
        font-size: 75%;
        text-align: center;
        color: white;
    }
    h1 {
        font-size: 24px;
        font-weight: bold;
    }
    h1 {
        font-weight: bold;
        line-height: 1;
        padding-bottom: 20px;
        font-family: Helvetica, sans-serif;
    }
    h2 {
        text-align: center;
        font-weight: bold;
        font-size: 27px;
    }
    p {
        text-align: center;
        font-size: 14px;
        margin: 0;
        padding: 0;
        color: white;
    }
    .explain {
        border-top: 1px solid #fff;
        border-bottom: 1px solid #fff;
        line-height: 1.5;
        margin: 30px auto;
        padding: 17px;
    }
    #cause {
        text-align: left;
    }
    #cause li {
        color: #666;
    }
    h3 {
        letter-spacing: 1px;
        font-weight: bold;
        padding: 0;
    }
    #white_box {
        margin: 15px auto 0;
        background-color: white;
    }
    
    /* ====================
    
      ???????????
    
    ======================= */
    @media only screen and (min-width: 0) and (max-width: 767px) {
    	#base {
    		padding: 30px 10px;
    	}
    	h1 {
    		font-size: 26px;
    	}
    	h1 span {
    		font-size: 60px;
    	}
    	h2 {
    		font-size: 16px;
    	}
    	.explain {
    		font-size: 14px;
    	}
    	h3 {
    		margin-top: 45px;
    		font-size: 16px;
    	}
    	#cause {
    		padding: 20px;
    		font-size: 12px;
    	}
    }
    
    /* ====================
    
       ???????????????
    
    ======================= */
    
    @media only screen and (min-width: 768px) {
    	#base {
    		margin-top: 80px;
    	}
    	h1 {
    		font-size: 50px;
    	}
    	h1 span {
    		font-size: 110px;
    	}
    	.explain {
    		font-size: 16px;
    		width: 660px;
    	}
    	#white_box {
    		width: 680px;
    		margin-bottom: 50px;
    	}
    	h3 {
    		font-size: 20px;
    		margin-top: 80px;
    	}
    	#cause {
    		padding: 30px;
    		font-size: 14px;
    	}
    }
    </style>
    </head>
    
    <body>
    <div id="base">
        <h1><span>403</span><br />
            Forbidden</h1>
        <h2>???????????褦?????????????????????????????</h2>
        <p class="explain">???Υ??顼????????????????Υ????????????????????????????????</p>
        <h3>????Τ褦?????????????????</h3>
        <div id="white_box">
            <div id="cause">
                <ul>
                    <li>???????????????????????????????????????????????????</li>
                    <li>???????????????index.html, index.htm ??????????????</li>
                </ul>
            </div>
        </div>
        <!--//base--></div>
    </body>
    </html>
    
    ?----------------------------------------------------------------------------------------------------------
    

    Hi @yasu7

    It is strange that you are getting an HTML output, as when I tested it with my Python program, I always get a JSON output. I will show you my output with a correct response and another with an incorrect one.

    Correct response

    python3 test.py -v
    
    {"id":40,"date":"2023-05-24T09:18:50","date_gmt":"2023-05-24T09:18:50","guid":{"rendered":"https:\/\/glotpress.test\/test-title-5\/","raw":"https:\/\/glotpress.test\/test-title-5\/"},"modified":"2023-05-24T09:18:50","modified_gmt":"2023-05-24T09:18:50","password":"","slug":"test-title-5","status":"publish","type":"post","link":"https:\/\/glotpress.test\/test-title-5\/","title":{"raw":"Test title","rendered":"Test title"},"content":{"raw":"Test content","rendered":"<p>Test content<\/p>\n","protected":false,"block_version":0},"excerpt":{"raw":"","rendered":"<p>Test content<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"permalink_template":"https:\/\/glotpress.test\/%postname%\/","generated_slug":"test-title-5","_links":{"self":[{"href":"https:\/\/glotpress.test\/wp-json\/wp\/v2\/posts\/40"}],"collection":[{"href":"https:\/\/glotpress.test\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/glotpress.test\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/glotpress.test\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/glotpress.test\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":0,"href":"https:\/\/glotpress.test\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"wp:attachment":[{"href":"https:\/\/glotpress.test\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/glotpress.test\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/glotpress.test\/wp-json\/wp\/v2\/tags?post=40"}],"wp:action-publish":[{"href":"https:\/\/glotpress.test\/wp-json\/wp\/v2\/posts\/40"}],"wp:action-unfiltered-html":[{"href":"https:\/\/glotpress.test\/wp-json\/wp\/v2\/posts\/40"}],"wp:action-sticky":[{"href":"https:\/\/glotpress.test\/wp-json\/wp\/v2\/posts\/40"}],"wp:action-assign-author":[{"href":"https:\/\/glotpress.test\/wp-json\/wp\/v2\/posts\/40"}],"wp:action-create-categories":[{"href":"https:\/\/glotpress.test\/wp-json\/wp\/v2\/posts\/40"}],"wp:action-assign-categories":[{"href":"https:\/\/glotpress.test\/wp-json\/wp\/v2\/posts\/40"}],"wp:action-create-tags":[{"href":"https:\/\/glotpress.test\/wp-json\/wp\/v2\/posts\/40"}],"wp:action-assign-tags":[{"href":"https:\/\/glotpress.test\/wp-json\/wp\/v2\/posts\/40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}

    Incorrect response (I changed the password, so the response is incorrect)

    python3 test.py -v
    
    {"code":"incorrect_password","message":"La contrase\u00f1a proporcionada es una contrase\u00f1a de aplicaci\u00f3n no v\u00e1lida.","data":{"status":401}}

    Can you try to show us this output?

    Thread Starter yasu7

    (@yasu7)

    Hi @amieiro

    Thank you for your help.
    When I ran python3 test.py -v at the command prompt, the following result was displayed.
    
    C:\●●●\●●●>python3 wp1.py -v
    <!DOCTYPE html>
    <html lang="ja">
    <head>
    <meta charset="EUC-JP" />
    <title>403 Forbidden</title>
    <meta name="copyright" content="Copyright XSERVER Inc.">
    <meta name="robots" content="INDEX,FOLLOW" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0">
    <style type="text/css">
    * {
        margin: 0;
        padding: 0;
    }
    img {
        border: 0;
    }
    ul {
        padding-left: 2em;
    }
    html {
        overflow-y: scroll;
        background: #3b79b7;
    }
    body {
        font-family: "????", Meiryo, "??? ?Х??????", "MS PGothic", "??饮?γ?? Pro W3", "Hiragino Kaku Gothic Pro", sans-serif;
        margin: 0;
        line-height: 1.4;
        font-size: 75%;
        text-align: center;
        color: white;
    }
    h1 {
        font-size: 24px;
        font-weight: bold;
    }
    h1 {
        font-weight: bold;
        line-height: 1;
        padding-bottom: 20px;
        font-family: Helvetica, sans-serif;
    }
    h2 {
        text-align: center;
        font-weight: bold;
        font-size: 27px;
    }
    p {
        text-align: center;
        font-size: 14px;
        margin: 0;
        padding: 0;
        color: white;
    }
    .explain {
        border-top: 1px solid #fff;
        border-bottom: 1px solid #fff;
        line-height: 1.5;
        margin: 30px auto;
        padding: 17px;
    }
    #cause {
        text-align: left;
    }
    #cause li {
        color: #666;
    }
    h3 {
        letter-spacing: 1px;
        font-weight: bold;
        padding: 0;
    }
    #white_box {
        margin: 15px auto 0;
        background-color: white;
    }
    
    /* ====================
    
      ???????????
    
    ======================= */
    @media only screen and (min-width: 0) and (max-width: 767px) {
            #base {
                    padding: 30px 10px;
            }
            h1 {
                    font-size: 26px;
            }
            h1 span {
                    font-size: 60px;
            }
            h2 {
                    font-size: 16px;
            }
            .explain {
                    font-size: 14px;
            }
            h3 {
                    margin-top: 45px;
                    font-size: 16px;
            }
            #cause {
                    padding: 20px;
                    font-size: 12px;
            }
    }
    
    /* ====================
    
       ???????????????
    
    ======================= */
    
    @media only screen and (min-width: 768px) {
            #base {
                    margin-top: 80px;
            }
            h1 {
                    font-size: 50px;
            }
            h1 span {
                    font-size: 110px;
            }
            .explain {
                    font-size: 16px;
                    width: 660px;
            }
            #white_box {
                    width: 680px;
                    margin-bottom: 50px;
            }
            h3 {
                    font-size: 20px;
                    margin-top: 80px;
            }
            #cause {
                    padding: 30px;
                    font-size: 14px;
            }
    }
    </style>
    </head>
    
    <body>
    <div id="base">
        <h1><span>403</span><br />
            Forbidden</h1>
        <h2>???????????褦?????????????????????????????</h2>
        <p class="explain">???Υ??顼????????????????Υ????????????????????????????? ???</p>
        <h3>????Τ褦?????????????????</h3>
        <div id="white_box">
            <div id="cause">
                <ul>
                    <li>???????????????????????????????????????????????????</li>
                    <li>???????????????index.html, index.htm ??????????????</li>
                </ul>
            </div>
        </div>
        <!--//base--></div>
    </body>
    </html>
    
    
    ※※※※※※※※ wp1.py ※※※※※※※※※※※※※
    import requests
    
    url = 'https://YYYYYYY.wp.xdomain.jp/wp-json/wp/v2/posts/'
    headers = {'Content-Type': 'application/json'}
    auth = ('YYYYYYY', 'XXXX XXXX XXXX XXXX XXXX XXXX')
    data = {
        'title': 'Test title',
        'content': 'Test content',
        'status': 'publish',
        'slug': 'test-title'
    }
    
    response = requests.post(url, headers=headers, auth=auth, json=data)
    print(response.text)
    
    ※※※※※※※※※※※※※※※※※※※※※

    It is very weird, because the response should be JSON, not HTML.

    I have created a local HTML file with your response, and it is not the regular 403 page from WordPress. Can you get more information from this screenshot? Could be related with your ISP or your hosting, making some additional block? It is weird, because the Bash command is working from the same machine :/

    Thread Starter yasu7

    (@yasu7)

    Hi @amieiro

    Thank you very much for all your help!
    
    >Can you get more information from this screenshot?
    thank you. Unfortunately, I wasn't able to get more information.
    
    
    >Could be related with your ISP or your hosting, making some additional block?
    I've done a lot of research, but I'm not sure why I'm getting a 403. I have checked the following settings, but is there anything else I should check?(What is your permalink setting?, etc)
    ?Application password is already set
    ?Restrictions on access to “WordPress REST API” from foreign IP addresses have been disabled
    
    
    Also, to solve the problem, I changed the program as follows and tried to run it. Do you have any clues?
    
    --------------- changed program ----------------------
    import requests
    
    url = 'https://YYYYYYY.wp.xdomain.jp/wp-json/wp/v2/posts/'
    headers = {'Content-Type': 'application/json'}
    auth = ('YYYYYYY', 'XXXX XXXX XXXX XXXX XXXX XXXX')
    data = {
        'title': 'Test title',
        'content': 'Test content',
        'status': 'publish',
        'slug': 'test-title'
    }
    
    response = requests.post(url, headers=headers, auth=auth, json=data)
    
    print("Response status code:", response.status_code)
    print("Response headers:", response.headers)
    print("Response text:")
    print(response.text)
    
    ---------------------------------------------------------
    
    
    ----------------Result-----------------------
    Response status code: 403
    Response headers: {'Connection': 'Keep-Alive', 'Keep-Alive': 'timeout=5, max=100', 'content-type': 'text/html; charset=UTF-8', 'last-modified': 'Fri, 21 Oct 2022 03:32:47 GMT', 'etag': '"b1b-635212df-b0abf8cad2eb7094;br"', 'accept-ranges': 'bytes', 'content-encoding': 'br', 'vary': 'Accept-Encoding', 'content-length': '1354', 'date': 'Sat, 27 May 2023 06:02:05 GMT', 'server': 'LiteSpeed', 'x-cache-status': '""', 'x-origin-date': '""', 'x-ua-device': '""', 'alt-svc': 'h3=":443"; ma=2592000, h3-29=":443"; ma=2592000, h3-Q050=":443"; ma=2592000, h3-Q046=":443"; ma=2592000, h3-Q043=":443"; ma=2592000, quic=":443"; ma=2592000; v="43,46"'}
    Response text:
    <!DOCTYPE html>
    <html lang="ja">
    <head>
    <meta charset="EUC-JP" />
    <title>403 Forbidden</title>
    <meta name="copyright" content="Copyright XSERVER Inc.">
    <meta name="robots" content="INDEX,FOLLOW" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0">
    <style type="text/css">
    * {
        margin: 0;
        padding: 0;
    }
    img {
        border: 0;
    }
    ul {
        padding-left: 2em;
    }
    html {
        overflow-y: scroll;
        background: #3b79b7;
    }
    body {
        font-family: "????", Meiryo, "??? ?Х??????", "MS PGothic", "??饮?γ?? Pro W3", "Hiragino Kaku Gothic Pro", sans-serif;
        margin: 0;
        line-height: 1.4;
        font-size: 75%;
        text-align: center;
        color: white;
    }
    h1 {
        font-size: 24px;
        font-weight: bold;
    }
    h1 {
        font-weight: bold;
        line-height: 1;
        padding-bottom: 20px;
        font-family: Helvetica, sans-serif;
    }
    h2 {
        text-align: center;
        font-weight: bold;
        font-size: 27px;
    }
    p {
        text-align: center;
        font-size: 14px;
        margin: 0;
        padding: 0;
        color: white;
    }
    .explain {
        border-top: 1px solid #fff;
        border-bottom: 1px solid #fff;
        line-height: 1.5;
        margin: 30px auto;
        padding: 17px;
    }
    #cause {
        text-align: left;
    }
    #cause li {
        color: #666;
    }
    h3 {
        letter-spacing: 1px;
        font-weight: bold;
        padding: 0;
    }
    #white_box {
        margin: 15px auto 0;
        background-color: white;
    }
    
    /* ====================
    
      ???????????
    
    ======================= */
    @media only screen and (min-width: 0) and (max-width: 767px) {
    	#base {
    		padding: 30px 10px;
    	}
    	h1 {
    		font-size: 26px;
    	}
    	h1 span {
    		font-size: 60px;
    	}
    	h2 {
    		font-size: 16px;
    	}
    	.explain {
    		font-size: 14px;
    	}
    	h3 {
    		margin-top: 45px;
    		font-size: 16px;
    	}
    	#cause {
    		padding: 20px;
    		font-size: 12px;
    	}
    }
    
    /* ====================
    
       ???????????????
    
    ======================= */
    
    @media only screen and (min-width: 768px) {
    	#base {
    		margin-top: 80px;
    	}
    	h1 {
    		font-size: 50px;
    	}
    	h1 span {
    		font-size: 110px;
    	}
    	.explain {
    		font-size: 16px;
    		width: 660px;
    	}
    	#white_box {
    		width: 680px;
    		margin-bottom: 50px;
    	}
    	h3 {
    		font-size: 20px;
    		margin-top: 80px;
    	}
    	#cause {
    		padding: 30px;
    		font-size: 14px;
    	}
    }
    </style>
    </head>
    
    <body>
    <div id="base">
        <h1><span>403</span><br />
            Forbidden</h1>
        <h2>???????????褦?????????????????????????????</h2>
        <p class="explain">???Υ??顼????????????????Υ????????????????????????????????</p>
        <h3>????Τ褦?????????????????</h3>
        <div id="white_box">
            <div id="cause">
                <ul>
                    <li>???????????????????????????????????????????????????</li>
                    <li>???????????????index.html, index.htm ??????????????</li>
                </ul>
            </div>
        </div>
        <!--//base--></div>
    </body>
    </html>
    
    ?
    --------------------------------------------
    Thread Starter yasu7

    (@yasu7)

    Hi @amieiro

    Thank you very much for your support. After changing the WordPress settings, the program ran successfully.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘【Python】About article posting program using WordPress REST API’ is closed to new replies.