How to get Confluence content through Personal Access Token, Proxy by Python? - python

I want to get content in Confluence with specific-page (pageID).
I've the script below:
import base64
import requests
import browser_cookie3
personal_access_token = "PATcode"
organizational_url = "https://confluence.fake-site.com/"
authorization = personal_access_token
headers = { 'Accept': 'application/json', 'Authorization': 'Bearer '+authorization }
confluence_url = organizational_url+'rest/api/content/pageID'
proxiesX = {
'http': '11.22.33.44:8080'
}
cookies = browser_cookie3.chrome(domain_name='.confluence.fake-site.com')
print(cookies)
response = requests.get(url=confluence_url, headers=headers, proxies = proxiesX, cookies=cookies, verify=False)
#print(response.status_code)
print(response.text)
My pageID has a content like this: "ABCDEF".
But upper script return output is un-structure HTML content with some context similar with "Unable to sign in, login failed..."
Can anyone help me to define where to fix?
Expecting output after running script: "ABCDEF" which is pageID's content

Related

Full authentication is required to access this resource error in python when I am trying to access a url using python requests

This is a python script to make a post call with respective header and body, but I am getting error Full authentication is required to access this resource
import requests
from requests.auth import HTTPBasicAuth
url = 'https://localhost:8000/bucket/items'
body = {
"serialNumbers": [
"AASD-ASD"
]
}
bearer_token = "07eb4900-016f-4523-a9b7-e5d6a87a7f8e"
jwt_token = "jhbasdfiunnX.faf-"
headers = {'content-type': 'application/json',
"authentication":"bearer "+ '07eb4900-a9b7-e5d6a87a7f8e',
"cookie":"jwt="+jwt_token}
headers = {"Cookie":"JWT="+jwt_token, "Authentication":"Bearer "+bearer_token, "Content-Type": "application/json" }
response = requests.post(url, body, auth=HTTPBasicAuth("abcd", "password"),headers=headers).json()
print(response)

How does Postman to generate this cookie?

import requests
url = "https://apiexample.com/load/v1/action/aaaaaaaaaaaaa"
payload={}
headers = {
'Authorization': 'OAuth oauth_consumer_key="aaaaaa",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1664837015",oauth_nonce="mKyTFn7OtsV",oauth_version="1.0",oauth_signature="aaaaaaaaaaaa"',
'Cookie': 'JSESSIONID=M7n-S-aGe8asRnTjNOUGowak5i5avsRBx6A4H8au.madsedepre'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
I am trying to retrieve information from an api, and I am using postman...
How can I know what postman is doing to generate that cookie??
and how can I generate it using python requests?

Empty body error on post request in python script

I'm trying to make a post request to an API endpoint with python and requests.
The endpoint requires a token. I get the token from the endpoint just fine.
When making a post request to the second endpoint Validation Error stating that body is empty.
import requests
url = "https://authz.dinero.dk/dineroapi/oauth/token"
payload = 'grant_type=password&username=****&password=****'
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ****'
}
response = requests.request("POST", url, headers=headers, data = payload)
r =response.json()
token = r['access_token']
url = "https://api.dinero.dk/v1/257403/contacts"
payload = {}
payload["Name"] = "Test Name"
payload["CountryKey"] = "DK"
payload["IsPerson"] = "true"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
}
response = requests.post(url, headers=headers, data = payload)
print(response.text)
This is the error I get:
{"code":42,"message":"Validation Error","validationErrors":{"Body":"The body was empty"},"languageSpecificMessages":[{"property":"message","message":"Der er fejl i de angivne data"},{"property":"Body","message":"The body was empty"}],"errorMessageList":[{"Code":"Body","Message":"The body was empty"}]}
Here is the same code taken from postman. It works fine.
import requests
url = "https://api.dinero.dk/v1/257403/contacts"
payload = "{\r\n \"Name\": \"Test Name\",\r\n \"CountryKey\": \"DK\",\r\n \"IsPerson\": true\r\n}"
print(payload)
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ****'
}
response = requests.request("POST", url, headers=headers, data = payload)
print(response.text.encode('utf8'))
I hope someone can explain why my code isn't working.
Requests has a json= param you could use:
response = requests.post(url, headers=headers, json=payload)
Docs here.
In your second call, you want to json dump the payload:
import json
response = requests.post(url, headers=headers, data=json.dumps(payload))
Postman has already serialised the payload as a json formatted string. You can do the same with json.dumps().

Not able to upload the data on thingworx with api

Tried many ways to upload the data(postman, httpie etc as given on their site) on thingworx but not able to do that.
Please have a look on the following code to upload the data on thingworx:
import requests
import json
app_key = 'xxxx'
url = 'http://pp-1804040542ze.devportal.ptc.io/Thingworx/Things/lmtech_thing/Properties/humidity'
prms = {'appKey': app_key}
hdrs = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
data = {'humidiy': '20'}
text = json.dumps(data)
print 'data: ' + text
r = requests.put(url, params=prms, headers=hdrs, data=text)
print r.status_code
Have created thing and key successfully. but it always return 404 error.
Tried with postman too. Here are the screenshots as shown below:
The following code worked for me :-)
import requests # Import requests library to send requests to Thingworx
url = 'http://52.199.28.120:8080/Thingworx/Things/work_thing/Properties/temp'
# temp is one of my property name
value = 12 # Upload 12 on Thingworx
headers = {
'Content-Type': 'application/json',
'appkey': 'xxxxxxxxxxxxxxxxxxxxxx',
'Accept': 'application/json',
'x-thingworx-session': 'true',
'Cache-Control': 'no-cache',
}
data = {"temp": value} # JSON data to upload on Thingworx
response = requests.put(url, headers=headers, json=data)
# Note that we have to send put request
print 'Response Code:', response.status_code
# If 200 then data has been uploaded successfully
print 'Response Content:', response.content

Exporting gooddata report python

Looking to connect to the Gooddata API and export a report via the API in python. The documentation is a bit confusing to follow.
I've defined a login to my instance of gooddata:
from urllib2 import Request, urlopen
import json
import requests
def login_gooddata(my_email, my_password):
url = 'https://secure.gooddata.com/gdc/account/login'
values = {
"postUserLogin": {
"login": my_email,
"password": my_password,
"remember": 0,
"verify_level": 0
}
}
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
encoded_values = json.dumps(values)
#request = Request(url, data=encoded_values, headers=headers)
r = requests.post(url, data=encoded_values)
return r
That successfully logs me in, returning a 200 response.
Given the documentation from the gooddata website on connecting to the API, I'm trying to export a raw project file.
I set the project and object ids:
project_id = 'asibfakuyebkbhdbfaisdf'
object_id = '87234760'
values = {
"report_req": {
"reportDefinition": "/gdc/md/"+ project_id + "/obj/" + object_id
}
}
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
url = 'https://secure.gooddata.com/gdc/app/projects/' + project_id + '/execute/raw/'
r = requests.post(url, data=json.dumps(values), headers=headers)
request = Request(url, data=json.dumps(values), headers=headers)
response_body = urlopen(requests).read()
print response_body
I played around with using r = requests.post(url, data=encoded_values and request = Request(url, data=encoded_values, headers=headers). Still receiving an error. I'm not really sure how to tackle next steps.
Following directions as stated in documentation for connecting to the API:
You need to perform all HTTP requests from a single "session" that remembers cookies from the login: perform s = requests.Session() once, then use s.post instead of requests.post.
See https://stackoverflow.com/a/31571805/3407728 for more.

Categories