我在以下问题上陷入困境:我有下面的API调用的代码。在和本身,API调用是好的,没有服务器问题,我已经检查了它在 Postman :
{
"@odata.context": "https://api.channeladvisor.com/v1/$metadata#Orders",
"value": [
{....}
],
"@odata.nextLink": "https://api.channeladvisor.com/v1/Orders?refresh_token=xxx&$skip=100"
}
我对这一切都是新手,所以请耐心等待。我所要做的就是在最后将所有记录保存到一个数据框中,而不仅仅是前100条记录。Channel Advisor的文档也不是很有帮助,而且我在Google上搜索时也找不到我需要的东西。
I found a post here,我对它进行了处理,以便添加到“while”循环中,我必须修改它以满足我的要求。
代码版本1(完整):
import requests
import pandas as pd
import os
import json
def get_access_token():
url = 'https://api.channeladvisor.com/oauth2/token'
headers = {'Authorization': 'Basic xxxxx'}
post_body = {'grant_type': 'refresh_token', 'refresh_token': 'xxxxx'}
response = requests.post(url, headers=headers, data=post_body)
return response.json()['access_token']
access_token = get_access_token()
url = 'https://api.channeladvisor.com/v1/orders'
headers = {
'Authorization': 'bearer ' + access_token,
'accept': 'text/plain',
'Content-Type': 'application/json'
}
response = requests.get(url, headers=headers)
response_data = response.json()['value']
response_nextlink = response.json()['@odata.nextLink']
#create list to store the values
data = []
data.extend(response_data)
while True:
if '@odata.nextLink' in response_nextlink:
response = requests.request('GET', response_data['@odata.nextLink'], headers=headers)
response_data = response.json()['value']
data.extend(response_data['value'])
response_nextlink = response.json()['@odata.nextLink']
else:
break
这段代码只运行一次,可能是因为while循环设置不正确。
因此,我添加了一些错误处理:
代码版本2:
while True:
response = requests.get(url, headers=headers)
if response.status_code != 200:
print("Error occurred. Status code: ", response.status_code)
print("Content: ", response.content)
break
response_data = response.json().get('value')
if not response_data:
break
data.extend(response_data)
if '@odata.nextLink' in response.json():
url = response.json()['@odata.nextLink']
else:
break
结果是状态代码400运行时错误。同样,当我尝试调用Postman时,服务器是正常的。
因此,我再次修改了while循环:
代码版本3:
while True:
response = requests.get(url, headers=headers)
response_data = response.json()['value']
data.extend(response_data)
if '@odata.nextLink' in response.json():
url = response.json()['@odata.nextLink']
else:
break
这将产生以下错误:
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
Input In [80], in <cell line: 25>()
24 while True:
25 response = requests.get(url, headers=headers)
---> 26 response_data = response.json()['value']
27 data.extend(response_data)
28 if '@odata.nextLink' in response.json():
...................
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
我被卡住了。我做错了什么?
1条答案
按热度按时间pdtvr36n1#
由于我没有API访问权限,我只能猜测您混淆了while循环中的break条件