Cannot seem to find nor create a minimum working example of posting an article to the REST API. I have the following set up and code.
import requests
import json
import base64
user = 'NASA'
password = 'bah bah ...'
url = 'http://any.com/wp-json/wp/v2'
data_sauth = user + ':' + password
post = {
'title': 'Rubbish',
'slug': 'rest-api-1',
'status': 'publish',
'content': 'random stuff',
'excerpt': 'lala',
'format': 'standard'
}
token = base64.standard_b64encode(data_sauth.encode())
headers = {'Authorization': 'Basic ' + token.decode('utf-8'), 'Content-Type': 'application/json'}
r = requests.post(url + '/posts', headers=headers, json=post)
print(r.status_code)
print(json.loads(r.content))
Now I get a 200 code. But I just get no post appearing and return of the posts. I cannot find where this might be going wrong. The API reference is here: https://developer.wordpress.org/rest-api/reference/posts/#create-a-post
On the WP side, I have installed the plugin named Application Passwords and ensured that the credentials match. No post appears, I get a 200 status code, and get a poll from the WP site of the existing posts, but no new post.
I know this is super old but I tried your code with my credentials and it worked perfectly, check that you're requesting to the right URL (some sites have different URLs for the API).
Related
So I am trying to post content through the wordpress rest api with my python script. The issue I am facing however is that when i do my post request i receive in response in json format all the posts already existing in the website instead of it creating a new post. Could someone please support here? My code looks like following;
import requests import json import base64
url = "https://myurl.com/wp-json/wp/v2/posts"
user = "Username"
password = "application password"
credentials = user + ':' + password
token = base64.b64encode(credentials.encode())
header = {'Authorization': 'Basic ' + token.decode('utf-8')}
post = { 'title' : 'Test WP-API',
'content' : 'This is my first post created using restAPI',
'status' : 'publish',
}
responce = requests.post(url , headers=header, json=post)
print(responce.json())
I have tried to google this issue anywhere but just cant seem anyone to having this issue. I would appreciate it soo much if someone could support.
I am trying to get data through API from amoCRM system but I am having some problems and I am really tired.
The url: https://alamiri.amocrm.ru/api/v4/contacts
This API should return contacts of users that contain (first name, email, number, ...).
When I copy and paste the above link in the browser, I get the data and everything works fine but when I try to get that data in Python it returns the following:
{'detail': 'Invalid user name or password',
'status': 401,
'title': 'Unauthorized',
'type': 'https://httpstatus.es/401'
}
my code in python (Django framework):
import requests
import pprint
def get_contacts():
url = "https://alamiri.amocrm.com/api/v4/contacts"
api_key = "def50200c1f82415f4625f96e402d967b880e33938b94d6ad8edd6c305cda8a3fff80990871fba6aac89391440c6330a26768410a52b0e332b2c6a1b2e49cbe912132194d4bb8c4a15bb304c14f649f1149826fc0eeaf1311f23feeea1118cba72af36e76c22f30fa2fb81a4f0678cde451492504ba56eff9eb7af876e49ad721105a9b8bcc94400bf21e9c9f5de43a17daa439d5af64a9ae8e54bf8c7005f4f01515cfe8756a0ddd209fd91c4ca15bc336ef89523876dd39f6472f190f82b1ecd4d9d0cfccc6941c1ffb64d79e2e6dbec1b12331154a7b343ac10abfc05f432d09da878d41a37cdacc50b1f18419594e669854efa108c17384143a71a4d6739bad60cdc704728e2c0dce82ff5707d8d6c8f34ce59aa1855a42153b3703635881fe1cc4a037070846a938a9d124845e1f217fd63c15ed99c79cbee2646b4df8018addaf76289708587a2b182747423e490c3ec21b9fd78c69cd73f2d8985cf916fd50b24e23a1a722d61449a8cd2dea4d1e4d334b103fb311cd1bf5454e0a979c52e65c303baa96d70d655f9a0a300a7ba78c5cc1aa0234d88eefd9bb11e245615257b8955f031f76cec978ed0e3eb3c479b83b91bef4158728d"
login = "my-email-that-I-used-to-create-account-on-amocrm"
client_uuid = "e1899afa-85a8-45ca-93f9-7f4b26374a5d"
client_secret = "jUlw4E6H417MVuzsujIW39GXXjpLjratAwOEiaqPr3KDBKYwc9ZQLXK2UlhMcsuu"
headers = {
'login': login,
'client_uuid': client_uuid,
'client_id': client_uuid,
'client_secret': client_secret,
'grant_type': 'authorization_code',
'Authorization': f'Bearer {api_key}',
'session_id': "a8f1uutp26uipj414pfudab8cv",
'refresh_token': "def50200e673c8cf44d3c3a28df221ffb9d97c2f25cffefa09337953dc1fcc9b9aaa479235f8a6840cde697ecb58252452a22a7a71696665ff19a515db602601743799729b799a6d98d743ba33ab23b1d750ae479a068d16dd9d503062023a8406728c55afb37be5c4cf36df5a3c93bf06c9ff097c54db83d90308a92999669c80506590df2e609fa2aa2d55f3066d06efde55ab0a3c64c44a87743c5db3115632a6ecbd498cbc8012ccddfeabfe7ad9ad0f01664e72c434ccacd686be0e9582567ebdf6abb0cd4258c3b16bbb487222be774086398930ef18f2181a8b6d184a9ce61454710cc3bcaf01e978067a059aaa16c322c8d7b0e5cf34069b0f3ff1e2f5e5a665ec798d6f3d05bb95912528ef8b3230c3e2d03fbd1bbc4087b4bca54d050f876589b7c9fbc1c45199a58b021afa832776ed22637ea1e940dbb3156acec1c0b6320066a2045e26dcd5b6b169fb773ca11ebbb398b513ef8f6f04f973506a8c01c48dad944aadf078db881e8f7de959c187ae7cc41135e8c151bd89e5b8d71c2dc20ee6f282fefd8cb93fcd0f0942ab20ae457da41ea22fed3d881d21585219defc611531ca3909a632095e6c1d568f2408f4eab7b4b8aa90cb892d2253925b8682b9dce4f5",
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImJkZDk0MTA1NTc5NWY0YzAwOTNmZDAwMmIxMDE1NDczZTZiN2ZjZDBlZGJlNWJlMWJjMjYzYTY2YTY2MGRlODA0OWQ5MjM5ZTE4ODY2MmQyIn0.eyJhdWQiOiI3YjQ5MGYxOC0yYmU1LTRhNTUtYTc0ZS0xOTYzYzk2NDFiOTgiLCJqdGkiOiJiZGQ5NDEwNTU3OTVmNGMwMDkzZmQwMDJiMTAxNTQ3M2U2YjdmY2QwZWRiZTViZTFiYzI2M2E2NmE2NjBkZTgwNDlkOTIzOWUxODg2NjJkMiIsImlhdCI6MTYzNzMzOTYzMywibmJmIjoxNjM3MzM5NjMzLCJleHAiOjE2MzczNDIwMzMsInN1YiI6Ijc2NDM4ODQiLCJhY2NvdW50X2lkIjpudWxsLCJzY29wZXMiOlsiY2hhdHMiLCJjcm0iLCJub3RpZmljYXRpb25zIiwidW5zb3J0ZWQiLCJtYWlsIl19.VEz7bIXpbsL-iMCz51UgW6JU1rySQTIXBLf_xPdBcmMpR3yIL8raUgCjEBJ05E05lhMsYxxwKEYg2yN468jkqfz5wTfmRdGs9ECiVatZ6oMEJYFUn7AF5JaP0fQw18raZdAo8CQJfZS86IuHEYpU2k-lW5LmDC2dIIyZYe4irnln0KRw5_mgci4JNPEyeqzYADI0deqX-rraXFjFm6UP4KkByfOrgfopyOV8cIBA_34i4CWewbVQLJF4-HWGuKvaOH5Erjh-GUNViyCCuH2Q4F7dI6gwhAd2z2lEbbTR_x4oyRx04QA1YOZjI64SJQAaaIYDFLphr7D4V59w44zG1g",
"csrf_token": "def50200e00c062021bac6b78d51f10f45cc28add00268d9",
}
response = requests.get(url, headers=headers)
pprint.pprint(response.json())
get_contacts()
I am reading their documentation (amoCRM) and I tried a lot of things but none worked! 😥
https://www.amocrm.com/developers/content/platform/abilities/
I followed their instructions how to work with their API:
https://nova-amocrm.notion.site/894277ffef5940e7a5daadd2abc746c8
But I am stuck and I don't know how to solve this problem!
Any help please?
If you don't know what is amoCRM, then
Firstly, you should create an integration in your amoCRM account and use your google colab notebook link to redirection (or any other link).
enter image description here
Next, use the request to get refresh code and access token:
data = {'client_id' : '3d31ef87-...',
'client_secret' : 'xyEFPbRH5xw1...',
'grant_type' : 'authorization_code',
'code' : 'def50200ec88c8fe724cfc58ebd7e19...',
'redirect_uri' : 'https://colab.research.google.com/drive/...'}
new_url = 'https://your_domain/oauth2/access_token'
request = requests.post(new_url, data=data)
request_dict = json.loads(request.text)
refresh_code = request_dict['refresh_token']
access_token = request_dict['access_token']
Next, define the request headers:
api_call_headers = {'Authorization': 'Bearer ' + access_token}
And after that you can get any information using filters:
api_statuses_response = requests.get('https://alamiri.amocrm.ru/api/v4/contacts?filter[id]={id}&filter[created_at][from]={from}&filter[created_at][to]={to}', headers=api_call_headers, verify=True)
print(api_statuses_response.json())
I'm trying to use the reddit API to pull text data for a machine learning project, but I can't get the OAuth access token. Instead of responding with the access token, the response just says bad request.
Here's my code:
import requests
auth = requests.auth.HTTPBasicAuth('*****', '****')
data = {'grant_type':'password',
'username' : '*****',
'password':'******'}
headers = {'User Agent': 'win64 : **** v1.0.0 by /u/*****'}
res = requests.post('https://www.reddit.com/api/v1/access_token', auth=auth, data=data, headers=headers)
print(res)
I've triple checked the personal use script and secret, and so I'm struggling to think of what's wrong. Any ideas?
im currently trying to login to my account at ds-exchange (https://app.ds-exchange.co.uk/#/login/signin) via Python (Dont ask why, im just testing what i can do with Paython currently since im learning it :P )
I've checked a bit how their login works with Fiddler:
They do an empty post # https://api.ds-exchange.co.uk/v1/get/ip/
They do post device_id, app_type, platform, app_version & identifier which they've got from step 1. # https://api.ds-exchange.co.uk/v1/device/register/
Now they post email, password(in sha256) and access_token # https://api.ds-exchange.co.uk/v1/login/
I've rewrite it simply in Python:
import re
import json
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
#disable ssl warning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
payload = {}
content = requests.post('https://api.ds-exchange.co.uk/v1/get/ip/', verify=False, params=payload)
print(content.text)
payload = {'device_id': '123123123123123', 'app_type': 'admin', 'platform': 'web', 'app_version': '2.0.1', 'identifier': '127.0.0.1'}
content = requests.post('https://api.ds-exchange.co.uk/v1/device/register/', verify=False, params=payload)
print(content.text)
answer = json.loads(content.text)
print(answer['data']['access_token'])
accesstoken = answer['data']['access_token']
payload = {'email': 'dolemites#test.de', 'password': 'dolemites', 'access_token': accesstoken}
content = requests.post('https://api.ds-exchange.co.uk/v1/login/', verify=False, params=payload)
print(content.text)
That are the answers i got from python script:
Step 1: {"ts":1534762909,"success":true,"error":false,"data":{"ip_address":"::1"}}
Step 2: {"ts":1534762911,"success":true,"error":false,"data":{"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl9pZHMiOiIyZTAxZmI0MDUyZTEyZjdmMzU2OTliZWVmODU3Zjk2MTY5ODBmMDA0MDMwOTFmM2M3OTk3NzdhYzRjZDM0YTEyIiwiaWF0IjoxNTM0NzYyOTExLCJleHAiOjE1MzQ4NDkzMTEsImF1ZCI6ImNyeXB0b2JhbmsuY29tIiwiaXNzIjoiY3J5cHRvYmFuay5jb20ifQ.aYAcdRmYdRPxYtylh17gYJKjXYKiRhWlWRg-JnmFYBw"}}
Step 3: {"ts":1534762912,"success":false,"error":true,"error_msg":"Access Denied. Token not found","logout":true}
The problem is that im getting a completly different answer in Fiddler in step 3, im getting there this: "error_msg": "dolemites#test.de is not found"
What im doing wrong? :(
Try to use selenium library, this is used for make unittest.
I am fairly new to programming and Python and I tried to POST to the wordpress REST API with OAuth1.0a to create a post to my blog.
In Postman it all works well, so the credentials are ok. But my Python code below returns 401 - rest_cannot_create.
I have been trying to make this work for 5 hours now. I hope you can help a newbie!
Thank you!
import requests
from requests_oauthlib import OAuth1
import json
url = "https://example.com/wp-json/wp/v2/posts/"
oauth_consumer_key = "1234567"
oauth_consumer_secret = "ABCDEFG"
oauth_token = "9876543"
oauth_token_secret = "HIJKLMNOP"
auth = OAuth1(oauth_consumer_key, oauth_consumer_secret, oauth_token, oauth_token_secret)
post = {'date': '2017-06-19T20:00:35',
'title': 'Testing API via Python',
'slug': 'rest-api-1',
'status': 'draft',
'content': 'this is the content post',
'excerpt': 'this is the excerpt',
'format': 'standard',
'author': "1"
}
r = requests.post(url, auth=auth, json=post)
print(json.loads(r.content))
I had to replace the last two lines with the following and it worked for me -
r = requests.post(url, auth=auth, data=post)
print(r.content)