requests.exceptions.HTTPError: 401 Client Error atlassian-python-api - python

I am trying to connect to a Confluence page using the python wrapper on the API (as I am not familiar with any of this) but I keep getting the following error:
requests.exceptions.HTTPError: 401 Client Error
I know that people talk about this being caused by the necessity of using an API token but the page runs on an old version of Confluence and I have been told that we cannot use access tokens.
So has anyone any other idea? Here's a small code:
from atlassian import Confluence
confluence = Confluence(
url='https://address',
username='name',
password='pwd'
)
confluence.create_page(
space='Test',
title='A title',
body='something')
I have tried to use an older version of atlassian-python-api just in case there was some conflict but it got me the same error.

Your code looks ok. Authenticating to Confluence using Basic Auth should work without generating an API token, afaik.
The 401 status definitely suggests a problem with the authentication though. The obvious reason for this would be of course wrong credentials, but I assume that you have double checked that the credentials work when interactively logging into confluence with a browser.
To get a better sense of the error, you can import logging to debug your requests and response:
from atlassian import Confluence
import logging
logging.basicConfig(filename='conf_connect.log', filemode='w', level=logging.DEBUG)
try:
c = Confluence(url='https://conf.yoursystem.com', username='name', password='pwd')
# atlassian API does not raise error on init if credentials are wrong, this only happens on the first request
c.get_user_details_by_username('name')
except Exception as e:
logging.error(e)
The Confluence module internally also uses logging, so the requests and responses will appear in your conf_connect.log logfile:
DEBUG:atlassian.rest_client:curl --silent -X GET -H 'Content-Type: application/json' -H 'Accept: application/json' 'https://conf.yoursystem.com/rest/api/user?username=name'
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): conf.yoursystem.com:443
DEBUG:urllib3.connectionpool:https://conf.yoursystem.com:443 "GET /rest/api/user?username=name HTTP/1.1" 401 751
DEBUG:atlassian.rest_client:HTTP: GET rest/api/user -> 401
DEBUG:atlassian.rest_client:HTTP: Response text -> <!doctype html><html lang="en"><head><title>HTTP Status 401 – Unauthorized</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 401 – Unauthorized</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> Basic Authentication Failure - Reason : AUTHENTICATED_FAILED</p><p><b>Description</b> The request has not been applied because it lacks valid authentication credentials for the target resource.</p><hr class="line" /><h3>Apache Tomcat/9.0.33</h3></body></html>
ERROR:root:401 Client Error: for url: https://conf.yoursystem.com/rest/api/user?username=name
The response body may include some information on the reason:
HTTP Status 401 – UnauthorizedType Status ReportMessage Basic Authentication Failure - Reason : AUTHENTICATED_FAILEDDescription The request has not been applied because it lacks valid authentication credentials for the target resource.
The reason AUTHENTICATED_FAILED suggests something is likely wrong with your credentials. If you want to dig deeper into that, you can use this SO answer to also display the headers that are being sent with your request.
However, if your reason is AUTHENTICATION_DENIED the problem is likely the following: If you have too many failed authentication attempts in a row, a CAPTCHA challenge is triggered, and this error will occur until the Failed Login Count is reset. This can easily happen when you are developing a script and test it frequently. To remedy this, either open a browser and manually (re-)logon to Confluence, completing the CAPTCHA, or resolve it from the Confluence User Management.

Related

Notion APi HTTPError: 403 Client Error: Forbidden for url: https://www.notion.so/api/v3/getUploadFileUrl

I am currently trying to informally connect to the Notion homepage to upload an image.
The reason for using informal API is that it does not allow us to upload images from our local folders.
With that said, I successfully connected to my homepage using token_v2 and created an image block using children.add_new('image').
However, when I tried to upload an image, I ran across 403 Client Error.
I've been trying to solve this issue for a few days so please let me know if there's anything I'm missing. Below is my code.
from notion.client import NotionClient
def uploadEvaluationJPG():
token_v2 = secret.notion_API("token_v2")
client = NotionClient(token_v2=token_v2)
# connect page
url = 'https://www.notion.so/Home-******************************'
page = client.get_block(url)
newchild = page.children.add_new('image')
newchild.upload_file(r"C:\NotionUpdate\progress\jpg files\Monthly Evaluation\month.jpg")
newchild.move_to(page.children[1],"before")
page.children[0].remove()
Error Code
Traceback (most recent call last):
Input In [8] in <cell line: 11>
newchild.upload_file(r"C:\NotionUpdate\progress\jpg files\Monthly Evaluation\month.jpg")
File ~\AppData\Roaming\Python\Python39\site-packages\notion\block.py:641 in upload_file
data = self._client.post(
File ~\AppData\Roaming\Python\Python39\site-packages\notion\client.py:265 in post
response.raise_for_status()
File ~\AppData\Roaming\Python\Python39\site-packages\requests\models.py:909 in raise_for_status
raise HTTPError(http_error_msg, response=self)
HTTPError: 403 Client Error: Forbidden for url: https://www.notion.so/api/v3/getUploadFileUrl
The code is correct, it appears that Cloudflare is blocking the requests before they reach Notion.
If we run the request in Postman using the cloud agent, we get a successful response 200. Choosing the desktop agent, though, we get the 403 Forbidden message. Furthermore, we can also preview the reason for it. It says:
Please stand by, while we are checking your browser...
So it seems that Notion/Cloudflare doesn't like the origin of the request.
Notion has an official API now, but sadly file upload is not implemented yet and we will have to wait for it. Many other things may start failing in the unofficial API if they tighten security.
To replicate in Postman:
url: https://www.notion.so/api/v3/getUploadFileUrl
body:
{"bucket":"secure","name":"envs.toml","contentType":"text/plain; charset=utf-8"}
For authentication, create a header called cookie with the value token_v2=your_token

403 (Forbidden) error when using Fortnite Tracker api with urllib.request

I am attempting to get user statistics from the Fortnite tracker api.
I have an api key and am using the correct url as indicated in the documentation
Template url:
https://api.fortnitetracker.com/v1/profile/{platform}/{epic-nickname}
Desired url:
https://api.fortnitetracker.com/v1/profile/pc/xantium0
If I use this link in browser I get {"message":"No API key found in request"} (as I have not passed the API key) so the link should be correct. Also if I do not pass the api key with urllib then I still get a 403 error.
I have checked out how to pass a header in a request: How do I set headers using python's urllib?
and so far have this code:
import urllib.request as ur
request = ur.Request('https://api.fortnitetracker.com/v1/profile/pc/xantium0', headers={'TRN-Api-Key' : 'xxx'})
response = ur.urlopen(request)
print(response.read())
When run I get this error:
urllib.error.HTTPError: HTTP Error 403: Forbidden
403 checks out as:
HTTP 403 is a standard HTTP status code communicated to clients by an HTTP server to indicate that the server understood the request, but will not fulfill it. There are a number of sub-status error codes that provide a more specific reason for responding with the 403 status code.
https://en.wikipedia.org/wiki/HTTP_403
The response is the same if I don't pass the api key in the header.
I can only think of three reasons this code is not working:
I have passed the wrong header name (i.e. it's not TRN-Api-Key)
My code is incorrect and I am not actually passing a header to the server
I have been banned
My problem is that I think my code is correct:
From the documentation:
urllib.request.Request(url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None)
I have passed the url and I have passed the headers (wihout confusing with the data arguement). The api documentation also mentions it should be passed in the headers.
I am also quite sure I need to use the TRN-Api-Key as it is shown in the api documentation:
TRN-Api-Key: xxx
Also in this question (using Ruby):
header = {
key: "TRN-Api-Key: Somelong-api-key-here"
}
Or I have been banned (this is possible although I got the key 15 minutes ago) is there a way to check? Would this error be returned?
What is preventing me from getting the user statistics?
Try using requests, a pythonic, fast and widely used module.
import requests
url = 'https://api.fortnitetracker.com/v1/profile/pc/xantium0'
headers = {
'TRN-Api-Key' : 'xxx'
}
response = requests(url, headers=headers)
print('Requests was successful:', response.ok)
print(response.text)
If it doesn't work you can visit the url with your browser, then check the requests:
in Firefox press Cntrl+Shift+E, in Chrome Cntrl+E (or Inspect with Cntrl+Shift+I and then go to Network). Press on "https://api.fortnitetracker.com/v1/profile/pc/xantium0" and change the headers. On Firefox there's the button Modify and resend. Check the response and eventually, try to change the header api key name.
Hope this helps, let me know.

failed API call - error 400

I've written a simple API call using requests and am getting an error 400 on executing the call. Can someone please tell me where I am going wrong? Thanks for the help. Here's the code i wrote -
import requests
params={
'api_key':'gozbsSP1fxqNSS5YjcFM7qcjjKch1tBB',
'api_secret':'HklHJCzfO87YyIC9DudGArVKJtioEhbO',
'image_url':'http://picz.in/data/media/7/study-in-canada-students.jpg'
}
r = requests.post(url='https://api-
us.faceplusplus.com/facepp/v3/detect',data=params)
print(r)
400 error code basically means it's a bad request. So it can be that you provided the wrong params for the api, or the api_key or api_secret is not correct.
Check the documentation of the API whether you are sending all the required params are there or not. If so check your secret keys and make sure it's the correct one.
Remeber to never post you API Keys on public forums like SO...
400 means its a bad request: the request you made is not what the server wanted.
(invalid arguments, wrong payload data, etc)
More info on 400 errors here:
https://airbrake.io/blog/http-errors/400-bad-request
The 400 Bad Request Error is an HTTP response status code that indicates that the server was unable to process the request sent by the client due to invalid syntax
Sometimes the response object will include information on why the request failed, but that's not always the case.

Requests get method giving 401 with auth

I am trying to get a response from an internal url which I can access through my laptop using a web-browser.
s = requests.Session()
r = s.get(url_1, auth=auth, verify=False)
print r.text
the reply i get is: 401 - unauthorized.
It's obviously going to be difficult to debug an HTTP 401 Unauthorized as we don't have access to the internal URL. Your code looks correct to me so I'm assuming this is a real 401 Unauthorized which means the request has incorrect authentication credentials. My advice would be to make sure you have reviewed the Python Requests docs on authentication and consider that your request is likely going through a proxy so the Requests docs on proxy config might be helpful.

Python API with Response - Authentication

Working with the Streak CRM API by using Python and Request. I am getting 4XX errors depending on how I phrase the request. (Examples below)
response = requests.get('https://www.streak.com/api/v1/pipelines?api-key=xxxxxxxx')
response.headers['Content-Type'] = 'application/json'
print(response.status_code)
print(response.content)
Result: 401 "Authentication Required for Streak API: try HTTP Basic Auth or cookie auth"
If I add ".json" after the file path:
requests.get('https://www.streak.com/api/v1/pipelines.json?api-key=xxxxxxxx')
Result: 400 "Invalid API path specified" So I am assuming that I was authenticated but just have a poorly defined file path.
But if I use the same file path and credentials entered in the terminal:
curl https://www.streak.com/api/v1/pipeline -u xxxxxxxx:
Works like a charm.
Why would the file path work in the terminal but not in python? Why do I need to add .json to my file path? New to using APIs so any help is appreciated. Thanks.
I don't know why, but rephrasing it like below worked. I received status code 200:
response = requests.get('https://www.streak.com/api/v1/pipelines', auth=('xxxxxx',''))
The API key goes in the username parameter, and the password parameter is left blank.
The error message tells you all you need to know. For Basic Auth you put the authorization info in the request headers, not in the url. When you add .json to a endpoint, that usually means that you want a JSON response. The reason why it worked with curl is because curl's -u flag is setting the Auth header.

Categories

Resources