在用Python学习脚本的过程中,我和我一直试图了解如何连接到API,特别是这个API:https://leagueapps.com/api-documentation/。公司给我提供了一个示例Python脚本,用于连接和使用它们的API,如下所示:
#!/usr/bin/env python
# Example of exporting registrations, members, and transactions with batched
# results. A limited number of results are returned in each response. It can
# vary based on the type, but is generally around 1000 records.
# ubuntu 16.04: sudo apt install python-jwt python-crypto python-requests
# untested: pip install pyjwt crypto requests2
import argparse
import time
import random
import jwt
import requests
parser = argparse.ArgumentParser()
parser.add_argument('--site-id', type=int, required=True)
parser.add_argument('--client-id', required=True, help='client id for site. Probably the same as the certificate filename basename')
parser.add_argument('--pem-file', required=True, help='filename for certificate key in PEM format')
parser.add_argument('--type', required=True, choices=['registrations-2','members-2','transactions-2', 'accountingCodes'], help='type of records to export')
parser.add_argument('--domain', default='leagueapps.io')
parser.add_argument('--auth', default='https://auth.leagueapps.io')
args = parser.parse_args()
if args.auth:
print("using auth server {}".format(args.auth))
auth_host=args.auth
if args.domain == 'lapps-local.io':
# for local testing the Google ESP isn't HTTPS
admin_host='http://admin.{}:8082'.format(args.domain)
else:
admin_host='https://admin.{}'.format(args.domain)
site_id=args.site_id
record_type=args.type
# Make a request to the OAuth 2 token endpoint with a JWT assertion to get an
# access_token
def request_access_token(auth_host, client_id, pem_file):
with open(pem_file, 'r') as f:
key = f.read()
now = int(time.time())
claims = {
'aud': 'https://auth.leagueapps.io/v2/auth/token',
'iss': client_id,
'sub': client_id,
'iat': now,
'exp': now + 300
}
assertion = jwt.encode(claims, key, algorithm='RS256')
auth_url = '{}/v2/auth/token'.format(auth_host)
response = requests.post(auth_url,
data={ 'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion': assertion })
if response.status_code == 200:
return response.json()['access_token']
else:
print('failed to get access_token: ({}) {}'.format(response.status_code, response.text))
return None
# Calculate seconds to sleep between retries.
#
# slot_time is amount of time to for each slot and is multiplied by the slot
# random calculated slot to get the total sleep time.
#
# max_slots can be used to put an upper bound on the sleep time
def exponential_backoff(attempts, slot_time = 1, max_slots = 0):
if max_slots > 0:
attempts = min(attempts, max_slots)
return random.randint(0, 2 ** attempts - 1) * slot_time
# Initialize the last-updated and last-id query parameters to be used between
# requests. These should be updated after processing each batch of responses
# to get more results.
last_updated = 0
last_id = 0
access_token = None
batch_count = 0
# Maximum number of retries for a request
max_attempts=5
attempts=0
while attempts < max_attempts:
attempts += 1
# Get an access_token if necessary
if access_token is None:
print('requesting access token: {} {}'.format(args.client_id, args.pem_file))
access_token = request_access_token(auth_host, args.client_id, args.pem_file)
if access_token is None:
break
print('access token: {}'.format(access_token))
params={'last-updated': last_updated, 'last-id': last_id}
# set the access token in the request header
headers={ 'authorization': 'Bearer {}'.format(access_token) }
response = requests.get('{}/v2/sites/{}/export/{}'.format(admin_host, site_id, record_type), params=params, headers=headers)
# access_token is invalid, clear so next pass through the loop will get a new one
if response.status_code == 401:
print('error({}): {}'.format(response.status_code, response.text))
access_token = None
# immediately retry since it should get a new access token
continue
# Request can be retried, sleep before retrying
if response.status_code == 429 or response.status_code >= 500:
# sleep an exponential back-off amount of time
wait_seconds = exponential_backoff(attempts, 1.42, 5)
print('retry in {} on error status ({}): {}'.format(wait_seconds, response.status_code, response.reason))
time.sleep(wait_seconds)
continue
# error on request that can't be retried
if response.status_code != 200:
print('unexpected error ({}): {}'.format(response.status_code, response.reason))
# reasonably some sort of coding error and retry is likely to fail
break
# get the actual response JSON data
records = response.json()
# No more records, exit.
if (len(records) == 0):
print('done.')
break
batch_count += 1
# successful request, reset retry attempts
attempts = 0
# process the result records and do useful things with them
print('processing batch {}, {} records'.format(batch_count, len(records)))
printFile = open("records.json","w+")
def remove_uni(s):
s2 = s.replace("u'", "'")
s2 = s2.replace('u"', '"')
return s2
printFile.write("[")
for record in records:
#print(remove_uni(str(record)));
#print('record id: {}, {}'.format(record['id'], record['lastUpdated']))
# track last_updated and last_id so next request will fetch more records
last_updated = record['lastUpdated']
last_id = record['id']
printFile.write(remove_uni(str(record)) + ",")
printFile.write("]")
printFile.close()我似乎无法让这段代码正常工作,而我得到的错误是:
usage: Main [-h] --site-id SITE_ID --client-id CLIENT_ID --pem-file PEM_FILE
--type {registrations-2,members-2,transactions-2,accountingCodes}
[--domain DOMAIN] [--auth AUTH]
Main: error: the following arguments are required: --site-id, --client-id, --pem-file, --type我已经尝试过如何传递参数的值,但我不清楚它们在这个脚本中的位置,并且在多次搜索和阅读教程之后找不到答案。
有人能告诉我如何解决这个问题,或者指出一些文章可以帮助我理解这个问题吗?我想知道是否应该先详细了解所有的示例代码意味着什么,但为了获得结果,我只想获得working...if,您认为我应该采用前一种方法,反之亦然,考虑到我是初学者,我也很想知道这一点。
谢谢!
加布
发布于 2022-08-16 15:53:11
因此,在@ the 85430的帮助下,我找到了解决方案:
因为an允许脚本使用通过命令行输入的参数值运行,所以在运行脚本时,需要在编辑器中运行脚本,以确保编辑器以某种方式输入这些值。在PyCharm中,您将运行->编辑配置,然后通过在“参数”字段中输入它们,在您的运行配置中输入所需的值。例如,对于我上面发布的案例,它将是--site-id (在这里键入SITE_ID )--客户端-id(在这里键入CLIENT_ID ) --pem-file (如果在同一个目录中输入PEM_FILE名称,或者将这里的目录位置放在PEM_FILE中)--类型(为您在you解析中定义的选项中选择的选项键入PEM_FILE类型。
输入参数并给配置一个适当的名称后,将其保存下来。然后,在Pycharm中,您可以选择在运行和测试脚本时运行该配置。
https://stackoverflow.com/questions/73300649
复制相似问题