英文:
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 = '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!
答案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>'
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)
#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>")
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)
*注意: <your secret key>,<your refresh token> 和 <user name>
从您的帐户仪表板获取。
https://platform.ironsrc.com/partners/account/apiDetails
英文:
From IronSource
documentation, two Authentications.
#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>'
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)
#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>")
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)
*Note : <your secret key>, <your refresh token> and <user name>
Get from your account dash board.
https://platform.ironsrc.com/partners/account/apiDetails
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论