Apigee console 具有 Jawbone 图像端点违反的 strict requirements around response headers,因此这些端点在该控制台中不可用。
在返回原始 PNG 数据时,将这些端点放在那里也没有多大价值。
相反,这里是一些使用 requests_oauthlib 模块的示例 Python 代码。
首先,您需要执行 OAuth 握手以获取令牌。第 1 步是转到 UP authorization_url,登录您的 UP 帐户并授予访问权限:
>>> import requests_oauthlib
>>> up = requests_oauthlib.OAuth2Session(client_id=client_id, scope=['sleep_read'], redirect_uri=redirect_uri)
>>> up.authorization_url('https://jawbone.com/auth/oauth2/auth')[0]
u'https://jawbone.com/auth/oauth2/auth?response_type=code&client_id=-bry_vwGDwE&redirect_uri=http%3A%2F%2F127.0.0.1%3A8080%2Fauth&scope=sleep_read&state=LV2BAPq3dIz6eKu3xz420Zujb5f911
授予访问权限后,UP API 会将用户重定向回您的应用程序。第 2 步是复制您被重定向回的 URL 并使用它来获取令牌:
>>> token = up.fetch_token('https://jawbone.com/auth/oauth2/token', authorization_response=REDIRECTED_URL, client_secret=client_secret)
此时,OAuth2Session 对象 up 已准备好查询 API(您也可以使用此保存的令牌稍后创建另一个 OAuth2Session 对象)。
所以让我们查询 sleeps 端点以获取 sleep_xid。
>>> sleepr = up.get('https://jawbone.com/nudge/api/v.1.1/users/@me/sleeps')
>>> sleep_xid = sleepr.json()['data']['items'][0]['xid']
现在我们有了一个特定睡眠事件的 xid,您可以获取它的图形图像,API 将其作为原始 PNG 数据返回:
>>> sleepimgr = up.get('https://jawbone.com/nudge/api/v.1.1/sleeps/{}/image'.format(sleep_xid))
>>> sleepimgr.text
u'\u2030PNG\r\n...