[Python] Bearer Authentication in Python

Discussão em 'Python' iniciado por Stack, Fevereiro 12, 2025.

  1. Stack

    Stack Membro Participativo

    I am running into a problem trying to rewrite the bearer authentication in Python from Java for the IronSource API. This is the code I am trying to replicate:

    $authURL = 'https://platform.ironsrc.com/partners/publisher/auth';
    $authHeaders = array(
    'secretkey: <e83defbasdasd9227a9d2a952b2c5ec8b02e>',
    'refreshToken: <ee453860sd9227a9d2a952b2c5e476iii3gh5>',
    );

    $curlClient = curl_init($authURL);
    curl_setopt($curlClient, CURLOPT_HTTPHEADER, $authHeaders);
    curl_setopt($curlClient, CURLOPT_RETURNTRANSFER, true);
    $bearerTokenResponse = curl_exec($curlClient);
    $bearerToken = str_replace('"','',$bearerTokenResponse);
    curl_close($curlClient);


    It seems like this should be very straightforward to pass the secretkey and refreshtoken that I have on the website, but my attempts keep failing. Here is what I have tried:

    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

    response = requests.get('https://platform.ironsrc.com/partners/publisher/auth', auth=BearerAuth('INSERT SECRET KEY HERE'))


    And Also this!

    `import requests endpoint = "https://platform.ironsrc.com/partners/publisher/auth"

    No Idea what data I am supposed to be posting here


    data = {"ip": "1.1.2.3"}

    headers = {"Authorization": "Bearer MYSECRETKEY"}

    print(requests.post(endpoint, data=data, headers=headers).json())`

    According to the IronSource website, I am expecting the bearer AUTH to be returned, and then I need to do another get request with the AuthHeader having the bearer token in it.

    Thanks!

    Continue reading...

Compartilhe esta Página