[Koha-devel] help: // Koha rest api error (401)

Eric Phetteplace ephetteplace at cca.edu
Tue Mar 7 18:16:36 CET 2023


Hi Esharat,

You should keep the listserv copied on replies so others can see the
conversation, learn from it, or respond.

I don't know what the issue could be then. Are you performing the same
requests from the RESTer extension, as in first getting a token and then
using it in an authorization header? Small note, 443 isn't a subdomain,
it's a port number. Your request would look like:

response = requests.get(f"https://{mydomain}:443/api/v1/quotes",
headers=auth2)

but it is also more likely to be 8443 if your HTTP port is 8080. But if the
RESTer client works with whatever you're doing, then the ports are not the
issue.

As I said, your code works for me as written, and if another client
successfully requests something from your server then it is configured
correctly. If you are not sharing the exact code you're running, that might
help, or if you show the actual response data (a 401 should still have some
data encoded in it) or any error stack traces.

Best,

ERIC PHETTEPLACE Systems Librarian, Libraries (he/him)

ephetteplace at 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
<https://ashaybythebay.com/>, Marcus Books
<https://www.facebook.com/marcus.books/>

:(){ :|: & };:


On Tue, Mar 7, 2023 at 9:04 AM Esharat Mia <esharat at esharat.me> wrote:

> Dear Phetteplace,
> Hope you are well. Thank you so much for your response.
>  I tried with 443 with sub domain. But I faced same issues. For your kind
> information, I tested my api using RESTer chrome extension, it worked. I
> enable all api setting from koha adminstration. Need any editional
> configuration in server side?
>
>
> ---- On Tue, 07 Mar 2023 22:56:23 +0600 * ephetteplace at cca.edu
> <ephetteplace at cca.edu> * wrote ----
>
> 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 at 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
> <https://ashaybythebay.com/>, Marcus Books
> <https://www.facebook.com/marcus.books/>
>
> :(){ :|: & };:
>
>
> On Tue, Mar 7, 2023 at 12:59 AM Esharat Mia <esharat at 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("http://123.49.46.157:8080/
> <http://123.49.46.157: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",*
>
> *}*
>
> *response = requests.post
> <http://requests.post>("http://123.49.46.157:8080/api/v1/oauth/token
> <http://123.49.46.157:8080/api/v1/oauth/token>", data=data)*
>
> *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",
> "http://123.49.46.157:8080/api/v1/patrons
> <http://123.49.46.157:8080/api/v1/patrons>", headers=auth2*
> *)*
>
> *print(response)*
>
> Regrads,
> Esharat Mia
>
>
> _______________________________________________
> Koha-devel mailing list
> Koha-devel at 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/
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.koha-community.org/pipermail/koha-devel/attachments/20230307/9aff18a4/attachment-0001.htm>


More information about the Koha-devel mailing list