I am having a problem with authentication error from Alfresco, maybe somebody could enlighten me while im trying to find log files from the mess that is Alfresco.
curl -uadmin:password -X POST https://hostname/alfresco/api/-default-/public/alfresco/versions/1/nodes/node_uuid/children -F filedata=#/home/user/file.pdf
Now when I am trying to use python requests(TM) it gives me a 401. Ive checked the requests and they both send the same Authentication hash
This is the python code:
def handle_row_for_request(row):
url = 'https://' + hostname + '/alfresco/api/-default-/public/alfresco/versions/1/nodes/' + base_uuid + '/children'
data = '/home/user/file.pdf'
with open(data, 'rb') as payload:
headers={"content-type": "multipart/form-data"}
response = requests.post(url, headers=headers, data=payload, auth=(username, password))
You are using the data argument, but you need to use the file argument:
def handle_row_for_request(row):
url = 'https://' + hostname + '/alfresco/api/-default-/public/alfresco/versions/1/nodes/' + base_uuid + '/children'
files = {'filedata': ('/home/user/file.pdf', open('/home/user/file.pdf', 'rb'))}
headers={"content-type": "multipart/form-data"}
response = requests.post(url, headers=headers, files=files, auth=(username, password))
Below is the Python code which works fine for the upload which also uses the basic auth. Please check the request params.
import json
import requests
url = "http://localhost:8080/alfresco/service/api/upload"
auth = ("admin", "admin")
files = {"filedata": open("/tmp/foo.txt", "rb")}
data = {"siteid": "test", "containerid": "documentLibrary"}
r = requests.post(url, files=files, data=data, auth=auth)
print(r.status_code)
print(json.loads(r.text))
There were 2 parts to the problem. First I needed to use the file argument instead of data. On top of that Alfresto wants to get 'filedata' string inside the file argument.
Secondly auth(user,pw) generated a hash that had a single = sign in the end and alfresco said 401. When I used cURL and got the password hash from there it had two = signs in the end. Weird right? But that worked. Instead of using auth(user,pw) I set the 'Authorization': 'Basic ${base64thing}' as the authentication.
response = requests.post(url, headers={'Authorization': 'Basic base64string'}, files={'filedata': open(/path/, 'rb')})```
Related
I've automating message sending in discord with a simple script in python
def sendMsg(token:str, channel:str, message:str):
url = 'https://discord.com/api/v9/channels/' + channel + '/messages'
data = {"content": message}
header = {"authorization": token}
r = requests.post(url, data=data, headers=header)
print(r.status_code)
but I want to automate also sending slash commands and there is were i get the error, i've tryed using the url url = 'https://discord.com/api/v9/interactions' maybe the error is in the url that need a channel id but looking with dev tools in the headers: Request URL: https://discord.com/api/v9/interactions the json im going to send gives some errors like true is undefined did you mean True same as null is undefined, i've just doble quoted that, do you know what is the error? the status code is always 400
full code:
import requests
import json
token = 'thIsIsMySecreTt0k3n'
channelId = '1015655803383988314'
with open('data_json.json','r') as f:
s = f.read()
print(s)
dta = s
f.close()
def sendMsg(token:str, channel:str, message:str):
url = 'https://discord.com/api/v9/channels/' + channel + '/messages'
data = {"content": message}
#data = dta
header = {"authorization": token}
r = requests.post(url, data=data, headers=header)
print(r.status_code)
def sendRank(token:str,channel:str):
url = 'https://discord.com/api/v9/interactions'
data = dta
header = {"authorization": token}
r = requests.post(url, data=data, headers=header)
print(r.status_code)
print(type(eval(dta)))
sendRank(token, channelId)
you may also need to see the data_json.json that try to send /rank to probot
{"type":2,"application_id":"282859044593598464","guild_id":"1003270895185436803","channel_id":"1003270896359845998","session_id":"0eaab6fb154d359c5665ca016f1eec11","data":{"version":"971443831096635452","id":"971443830870126634","name":"rank","type":1,"options":[],"application_command":{"id":"971443830870126634","application_id":"282859044593598464","version":"971443831096635452","default_permission":"true","default_member_permissions":"null","type":1,"name":"rank","description":"View your rank card or someone else's in the server.","dm_permission":"true","options":[{"type":6,"name":"user","description":"User to get rank of."}]}}}
or the raw json without beeing modified
{"type":2,"application_id":"282859044593598464","guild_id":"1003270895185436803","channel_id":"1015655803383988314","session_id":"0eaab6fb154d359c5665ca016f1eec11","data":{"version":"971443831096635452","id":"971443830870126634","name":"rank","type":1,"options":[],"application_command":{"id":"971443830870126634","application_id":"282859044593598464","version":"971443831096635452","default_permission":true,"default_member_permissions":null,"type":1,"name":"rank","description":"View your rank card or someone else's in the server.","dm_permission":true,"options":[{"type":6,"name":"user","description":"User to get rank of."}]},"attachments":[]},"nonce":"1015670347103469568"}
Change data=data to json=data, you should get a 204 response code which means it worked.
You also do not need all of that data, the only ones you need are below
{"type":2,"application_id":"","guild_id":"","channel_id":"","session_id":"","data":{"version":"","id":"","name":""}}
Session id does not have to be a vaild id, it just has to be a string that isn't empty.
I'm having a issue when making a python GET to a API using requests lib and a payload, the error returned is a 400 "The types 'Edm.Boolean' and 'Edm.String' are not compatible.", see the code below.
import requests
url = "https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativaMercadoMensais"
querystring = {"$format":"json","$top":"1","$filter":'DataReferencia eq 06/2022 and Indicador eq Câmbio'}
headers = {'Cookie': 'TS01d9825e=0198c2d644d1afbffdff044aa68d6a5088fc806b2e1ac62e24367e61bb3c10a8c2b7eec94d00069c6d44269e2653616599c149f905; dtCookie=944AB33FFF88A610C23EBA9EBA852F9B|ZXhwZWN0YXRpdmFzfDE; BIGipServer~was-p_as3~was-p~pool_was-p_default_443=1020268972.47873.0000; JSESSIONID=00000xsbngFt_CzrLQkgb15FC3A:1cn7m3fq4; TS013694c2=0198c2d6446fb34209160d9d557965883a3a94328644e3b0278c6e4b3db9ee1bfc5903a5df8e1f1db029ba9f5d4d775874538a8854'}
response = requests.request("GET", url, headers=headers, params=querystring)
The headers I've extracted direct from the Postman tool!
When I use print(response.url) the returned URL is the following (note the + sign formatting)
https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativaMercadoMensais?%24format=json&%24top=1&%24filter=DataReferencia+eq+06%2F2022+and+Indicador+eq+C%C3%A2mbio
When I do not use the payload format and use the whole URL (like extracted from postman) it works, see below.
import requests
url = "https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativaMercadoMensais?$format=json&$top=1&$filter=DataReferencia eq '06/2022' and Indicador eq 'Câmbio'"
payload={}
headers = {}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
But the URL in this way come like this,
https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativaMercadoMensais?$format=json&$top=1&$filter=DataReferencia%20eq%20'06/2022'%20and%20Indicador%20%20eq%20'C%C3%A2mbio'
Instead of + signs composing the URL it's encoded with %20, so I'm wondering it's a URL encoding problem, but I can't get it right!
I tried to send file with some headers like:
files = {'file': (file, open(file, 'rb'), {'Content-type': 'multipart/form-data; boundary=---BOUNDARY', 'Authorization' : 'Basic ' + api_key})}
r = requests.post(base_url, files=files)
Server returned 401 error that means absent header Authorization. But I sent it
For Basic Authentication you can follow the requests docs. It's visible on the very first line of code in the example on that page.
Use the auth keyword argument to supply a 2-tuple of username and password:
response = requests.post(base_url, files=files, auth=('username', 'password'))
Edit:
If you want to send actual headers, rather than things like Basic Auth, you can do that with the headers keyword argument. This allows you to give a dict of headers you would like to send. For example:
headers = {'Content-Type': 'application/json'}\
response = requests.post(url, data=data, headers=headers)
The auth argument, should be a simplification of the above because Basic Auth is so common, but don't quote me on that one.
You can follow this example to take it from the official documentation I think you should try it first in postman, with the json then and do it from python.
This information is taken from the official documentation
python
i'm working on settin up a rest api with python, however i'm having some problem getting it to work.
I'm working with the TV DB rest api: https://api.thetvdb.com/swagger
and using python with Requests library to pull out the information.
My code is currently:
import json
import requests
URL = "https://api.thetvdb.com/"
API_KEY = "Api_key"
USER_KEY = "Key"
USERNAME = "Name"
headers = {"Accept": "application/json"}
params = {
"apikey": API_KEY,
"userkey": USER_KEY,
"username": USERNAME
}
resp = requests.post(URL + "login/", headers = headers ,params=params)
if resp.status_code != 200:
print('error: ' + str(resp.status_code))
else:
print('Success')
So far i'm only getting error code 401, not sure why.
Solved:
2 Things needed to be changed
1. The resp was changed into:
resp = requests.post(URL + "login/", headers = headers, data=json.dumps(params))
The header had to have
"Content-Type": "application/json"
added to it :) It's now working, thanks everyone
The login parameters probably need to be a JSON-encoded string POSTed as the body of the message.
Try resp = requests.post(URL + "login/", headers = headers, data=json.dumps(params))
I'm trying to make a simple post request via the requests library of Python and I get a bad request error (400) while my url is supposedly correct since I can use it to perform a get.
I'm very new in REST requests, I read many tutorials and documentation but I guess there are still things I don't get so my error could be basic. Maybe a lack of understanding on the type of url I'm supposed to send via POST. Here my code :
import requests
v_username = "username"
v_password = "password"
v_headers = {'content-type':'application/rdf+xml'}
url = 'https://my.url'
params = {'param': 'val_param'}
payload = {'data': 'my_data'}
r = requests.post(url, params = params, auth=(v_username, v_password), data=payload, headers=v_headers, verify=False)
print r
I used the example of the requests documentation.
I had a similar problem, i tried changing params to data or with json.dumps():
from json import dumps
r = requests.post(url, params=dumps(params), auth=(v_username, v_password), data=payload, headers=v_headers, verify=False)
or
r = requests.post(url, data=dumps(params), auth=(v_username, v_password), data=payload, headers=v_headers, verify=False)