1

I am trying to retrieve a sleep graph on a specific night, in https://jawbone.com/up/developer/endpoints/sleeps, the list includes getting a user's sleep graph.

However, in API console, there are only 5 Sleep Endpoint methods available, in which none are about getting sleep graph. The closest I can see is Specific Sleep Event. I've tried changing the request URL so that there's '/image' at the end as listed in the earlier link, but that just gets included into the sleep_xid value.

I would like to know if there's a way, and if so how. Thank you

Thone
  • 11
  • 2
  • This is an issue with how the Apigee console interacts with the UP API. I'm working on it, and will put in a full answer soon. Have you tried grabbing the sleep graph using code or a different interface? – RAY Jun 23 '16 at 00:29

1 Answers1

0

The Apigee console has strict requirements around response headers that the Jawbone image endpoints violate, so those endpoints are not available from that console.

There also would not be much value to having those endpoints there as they return raw PNG data.

Instead, here is some sample Python code using the requests_oauthlib module.

First, you need to perform the OAuth handshake to get a token. Step 1 is to go to the UP authorization_url, login to your UP account, and grant access:

>>> 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

After granting access, the UP API will redirect the user back to your application. Step 2 is to copy that URL you're redirected back to and use it to get a token:

>>> token = up.fetch_token('https://jawbone.com/auth/oauth2/token', authorization_response=REDIRECTED_URL, client_secret=client_secret)

At this point, the OAuth2Session object up is ready to query the API (you can also use this saved token to create another OAuth2Session object later).

So let's query the sleeps endpoint to get a sleep_xid.

>>> sleepr = up.get('https://jawbone.com/nudge/api/v.1.1/users/@me/sleeps')
>>> sleep_xid = sleepr.json()['data']['items'][0]['xid']

Now that we have an xid for a particular sleep event, you can get its graph image, which the API returns as raw PNG data:

>>> sleepimgr = up.get('https://jawbone.com/nudge/api/v.1.1/sleeps/{}/image'.format(sleep_xid))
>>> sleepimgr.text
u'\u2030PNG\r\n...
RAY
  • 474
  • 4
  • 21