Hi Esharat,

Your code should work but it has a few things that don't make sense.

Are you sure you have the setting "RESTOAuth2ClientCredentials" on your Koha? That is one guess.

My other thought is that you are requesting an HTTPS resource from a URL on port 8080. This looks like you might be running Koha locally; usually, HTTP requests are to port 80 and HTTPS to 443 and when you run things locally those are often shifted up eight thousand spots to 8080 and 8443. So basically—are you sure your server sends HTTPS responses on port 8080? Are you able to load https://{mydomain}:8080 in a browser? You said you're getting a 401 error, which means you're get a response, so this seems unlikely to be the problem (you would see a connection error and not receive a response if it was).

I was able to test successfully with your code so I don't necessarily think these are issues either, but there are a few pieces that don't make sense. The HTTPS connection that's initialized at the top is never used. As far as I know, the requests library doesn't magically pick up an existing connection and use it, nor is this necessary (requests will make a connection for you). Also, your second request uses a "content-type: application/json" HTTP header but it is a GET request, content-type is a header for the type of data you're sending but GET requests do not send anything. You probably meant to have an "accept: application/json" header instead. But again, I tried on our instance with that code and Koha did not seem to mind.

Best,

ERIC PHETTEPLACE Systems Librarian, Libraries (he/him)

ephetteplace@cca.edu


CCA is situated on the traditional unceded lands of the Chochenyo and Ramaytush Ohlone peoples.

Black-owned bookstores in Oakland: Ashay by the Bay, Marcus Books

:(){ :|: & };:



On Tue, Mar 7, 2023 at 12:59 AM Esharat Mia <esharat@esharat.me> wrote:
Dear All,
Hope this email finds you well. I am facing a error koha rest api , I explained below -
I use the below code base for the koha rest api . When I check the bywater koha demo site's rest api, it's working fine but When I checked my demo koha site, getting authentication error(401)
I entered the correct client id and secret ssl certificate installed correctly. My api worked properly using the chrome RESTer tool. Thanks in advance .

import requests
import json
import os
from requests.structures import CaseInsensitiveDict
import http.client
conn = http.client.HTTPSConnection("https://******:8080")

#from dotenv import load_dotenv

#load_dotenv()
data = {
"client_id": "e877bce0-78bc-4d39-a0c1-2df7fccb1397",
"client_secret": "461cda1a-665d-4d24-a0e0-dace5f83a1f4",
"grant_type": "client_credentials",

}


credentials = response.json()
print(credentials)
access_token = credentials['access_token']
print("Access_token: ",access_token)
#auth2 = CaseInsensitiveDict()
auth2={"content-type":"application/json","authorization": f"Bearer {access_token}"}

print(auth2)
response = requests.request("GET", "https://{mydomain}:8080/api/v1/patrons", headers=auth2
)

print(response)

Regrads,
Esharat Mia


_______________________________________________
Koha-devel mailing list
Koha-devel@lists.koha-community.org
https://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-devel
website : https://www.koha-community.org/
git : https://git.koha-community.org/
bugs : https://bugs.koha-community.org/