Search code examples
pythonapiauthentication

Bearer Authentication in Python


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!


Solution

  • From IronSource documentation, two Authentications.

    enter image description here

    #1 Bearer API Authentication

    curl command

    curl --silent --location 'https://platform.ironsrc.com/partners/publisher/auth' \
    --header 'secretkey: <your secret key>' \
    --header 'refreshToken: <your refresh token>'
    

    Result enter image description here

    Python code

    import requests
    
    url='https://platform.ironsrc.com/partners/publisher/auth'
    headers = {
        'secretkey': '<your secret key>',
        'refreshToken': '<your refresh token>'
    }
    response = requests.get(url, headers = headers)
    print(response.content)
    

    Result enter image description here

    #2 Standard API Authentication

    Simular but you needs to add more information

    curl --silent --location 'https://platform.ironsrc.com/partners/publisher/auth' \
    --header 'secretkey: <your secret-key>' \
    --header 'refreshToken: <your refresh token>' \
    --header 'Authorization: Basic '$(base64 <<<"<your user name>:<your secret-key>")
    

    Result enter image description here

    Python Code

    import requests
    from requests.auth import HTTPBasicAuth
    
    url='https://platform.ironsrc.com/partners/publisher/auth'
    headers = {
        'secretkey': '<your secret key>',
        'refreshToken': '<your refresh token>'
    }
    response = requests.get(url, headers = headers, auth= HTTPBasicAuth('<your user name>', '<your secret-key>'))
    print(response.content)
    

    Result enter image description here

    *Note : , and Get from your account dash board.

    https://platform.ironsrc.com/partners/account/apiDetails
    

    enter image description here

    enter image description here