我是 python 的新手,我无法解决以下问题。 我想从 discovergy API 下载智能电表读数。
首先,我使用以下命令获得包含所有仪表的列表:
response = session.get(base_url+'meters', header_auth=True)
然后我想使用以下命令获取一个智能电表的特定读数:
Test= session.get(base_url + 'readings?meterId=' + ZählerId + '&fields=energy&from='+str(start) +'&to='+str(ende)+'&resolution=fifteen_minutes',header_auth=True)
url 与 discovergyDocumentation (https://api.discovergy.com/docs/#/Readings) 中的相同,并且在浏览器中独立运行。 header_auth 语句在第一个命令中有效,但我仍然收到 HTTP401 错误。你能告诉我为什么以及如何解决这个问题吗?
(当然,一开始我得到了请求令牌。这是代码的其余部分:)
# Get consumer key and secret (not part of OAuth 1.0)
consumer_token_url = base_url + 'oauth1/consumer_token'
response = requests.post(url=consumer_token_url, data={'client': client_name})
if not response:
print('Error during consumer_token Request\n'
+ ' HTTP Status Code: ' + str(response.status_code) + '\n'
+ ' HTTP Response: ' + str(response.content))
consumer_key = response.json()['key']
consumer_secret = response.json()['secret']
print('Consumer Key: ' + consumer_key)
print('Consumer Secret: ' + consumer_secret)
discovergy = OAuth1Service(
name='discovergy',
consumer_key=consumer_key,
consumer_secret=consumer_secret,
request_token_url=base_url + 'oauth1/request_token',
access_token_url=base_url + 'oauth1/access_token',
authorize_url=base_url + 'oauth1/authorize',
base_url=base_url)
# Get OAuth request token
request_token, request_token_secret = discovergy.get_request_token(method='POST')
print('Request Token: ' + request_token)
print('Request Token Secret: ' + request_token_secret)
# Call authorize URL with email and password to get OAuth verifier
authorize_url = discovergy.get_authorize_url(request_token)
authorize_url += '&' + urlencode({'email': email, 'password': password})
print('Authorize URL: ' + authorize_url)
response = requests.get(url=authorize_url)
if not response:
print('Error during authorize_url Request\n'
+ ' HTTP Status Code: ' + str(response.status_code) + '\n'
+ ' HTTP Response: ' + str(response.content))
idx = str(response.content).find('oauth_verifier=')
if idx > -1:
oauth_verifier = str(response.content)[idx + 15:-1]
print('OAuth Verifier: ' + oauth_verifier)
# Get OAuth access token
session = discovergy.get_auth_session(request_token,
request_token_secret,
method='POST',
data={'oauth_verifier': oauth_verifier})
print('Access Token: ' + session.access_token)
print('Access Token Secret: ' + session.access_token_secret)
从 API 获取智能仪表读数