Seite 1 von 1

Graylog -> API-Test-Call with python

Verfasst: Montag 5. August 2024, 13:03
von kai_n1986
Hi Guys,
are here some guys, which are familar with api-calls over python especially graylog?
At the moment i try to implement a small test-api-call with python (see below).
With curl i get a valide response as json-object. With python i get a response, that my authorization ist not valid (401-Response.) See code below.
Can somebody help me or give me some hints, thank you!: The token is valide, because curl is working for me.


Gerne auch in Deutsch antworten! :)
-----------------------------------------------------------------------------------------------------
import requests

api_url=https://172.168.98.208:9000/api/system?pretty=true

bearer_token=‘11111111111111111111’ #dummy-token

headers = {“Authorization:” “{bearer_token}”}

response = requests.get(api_url, headers=headers, verify=False)

print(response.status_code)
--------------------------------------------------------------------------------------------

Grüße Kai

Re: Graylog -> API-Test-Call with python

Verfasst: Montag 5. August 2024, 13:35
von __blackjack__
@kai_n1986: Das ist offensichtlich nicht der Code der irgendwo mal gelaufen ist weil 3 von den 6 Zeilen mit Code Syntaxfehler enthalten.

Re: Graylog -> API-Test-Call with python

Verfasst: Montag 5. August 2024, 13:45
von DeaD_EyE
Das Format ist: "Authorization: Bearer TOKEN"

Code: Alles auswählen

import requests


api_url = "https://172.168.98.208:9000/api/system?pretty=true"
bearer_token = "11111111111111111111"
headers = {"Authorization": f"Bearer {bearer_token}"}

response = requests.get(api_url, headers=headers, verify=False)
print(response.status_code) 
Du kannst aber auch eine Klasse für die Baerer Authorization erstellen (code von https://stackoverflow.com/a/29931730):

Code: Alles auswählen

import requests


class BearerAuth(requests.auth.AuthBase):
    def __init__(self, token):
        self.token = token
    def __call__(self, r):
        r.headers["authorization"] = "Bearer " + self.token
        return r


api_url = "https://172.168.98.208:9000/api/system?pretty=true"
bearer_token = "11111111111111111111"
response = requests.get(api_url, auth=BearerAuth(bearer_token), verify=False)

Code nicht getestet.