Bearer Authentication in Python

huangapple go评论82阅读模式
英文:

Bearer Authentication in Python

问题

以下是您要翻译的内容:

Java 代码部分:

$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);

Python 代码部分:

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'))

Python 代码部分2:

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

### 不知道我应该发布什么数据 ###
data = {"ip": "1.1.2.3"}

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

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

请注意,Python 代码中的字符串部分已经从 HTML 编码转为普通文本。

英文:

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 = &#39;https://platform.ironsrc.com/partners/publisher/auth&#39;;
$authHeaders = array(
        &#39;secretkey: &lt;e83defbasdasd9227a9d2a952b2c5ec8b02e&gt;&#39;,
        &#39;refreshToken: &lt;ee453860sd9227a9d2a952b2c5e476iii3gh5&gt;&#39;,
);

$curlClient = curl_init($authURL);
curl_setopt($curlClient, CURLOPT_HTTPHEADER, $authHeaders);
curl_setopt($curlClient, CURLOPT_RETURNTRANSFER, true);
$bearerTokenResponse = curl_exec($curlClient);
$bearerToken = str_replace(&#39;&quot;&#39;,&#39;&#39;,$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[&quot;authorization&quot;] = &quot;Bearer &quot; + self.token
        return r

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

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!

答案1

得分: 0

#1 Bearer API Authentication

curl命令

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

结果
Bearer Authentication in Python

Python代码

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)

结果
Bearer Authentication in Python

#2 Standard API Authentication
类似,但您需要添加更多信息

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>")

结果
Bearer Authentication in Python

Python代码

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)

结果
Bearer Authentication in Python

*注意: <your secret key>,<your refresh token> 和 <user name>
从您的帐户仪表板获取。

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

Bearer Authentication in Python

Bearer Authentication in Python

英文:

From IronSource documentation, two Authentications.

Bearer Authentication in Python

#1 Bearer API Authentication

curl command

curl --silent --location &#39;https://platform.ironsrc.com/partners/publisher/auth&#39; \
--header &#39;secretkey: &lt;your secret key&gt;&#39; \
--header &#39;refreshToken: &lt;your refresh token&gt;&#39;

Result
Bearer Authentication in Python

Python code

import requests

url=&#39;https://platform.ironsrc.com/partners/publisher/auth&#39;
headers = {
    &#39;secretkey&#39;: &#39;&lt;your secret key&gt;&#39;,
    &#39;refreshToken&#39;: &#39;&lt;your refresh token&gt;&#39;
}
response = requests.get(url, headers = headers)
print(response.content)

Result
Bearer Authentication in Python

#2 Standard API Authentication

Simular but you needs to add more information

curl --silent --location &#39;https://platform.ironsrc.com/partners/publisher/auth&#39; \
--header &#39;secretkey: &lt;your secret-key&gt;&#39; \
--header &#39;refreshToken: &lt;your refresh token&gt;&#39; \
--header &#39;Authorization: Basic &#39;$(base64 &lt;&lt;&lt;&quot;&lt;your user name&gt;:&lt;your secret-key&gt;&quot;)

Result
Bearer Authentication in Python

Python Code

import requests
from requests.auth import HTTPBasicAuth

url=&#39;https://platform.ironsrc.com/partners/publisher/auth&#39;
headers = {
    &#39;secretkey&#39;: &#39;&lt;your secret key&gt;&#39;,
    &#39;refreshToken&#39;: &#39;&lt;your refresh token&gt;&#39;
}
response = requests.get(url, headers = headers, auth= HTTPBasicAuth(&#39;&lt;your user name&gt;&#39;, &#39;&lt;your secret-key&gt;&#39;))
print(response.content)

Result
Bearer Authentication in Python

*Note : <your secret key>, <your refresh token> and <user name>
Get from your account dash board.

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

Bearer Authentication in Python

Bearer Authentication in Python

huangapple
  • 本文由 发表于 2023年3月4日 02:59:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定