Graylog -> API-Test-Call with python

Sockets, TCP/IP, (XML-)RPC und ähnliche Themen gehören in dieses Forum
Antworten
kai_n1986
User
Beiträge: 1
Registriert: Montag 5. August 2024, 12:57

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
Benutzeravatar
__blackjack__
User
Beiträge: 14336
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

@kai_n1986: Das ist offensichtlich nicht der Code der irgendwo mal gelaufen ist weil 3 von den 6 Zeilen mit Code Syntaxfehler enthalten.
„Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.“ — Brian W. Kernighan
Benutzeravatar
DeaD_EyE
User
Beiträge: 1330
Registriert: Sonntag 19. September 2010, 13:45
Wohnort: Hagen
Kontaktdaten:

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.
sourceserver.info - sourceserver.info/wiki/ - ausgestorbener Support für HL2-Server
Antworten