Giter Site home page Giter Site logo

jrmycanady / nokiahealth Goto Github PK

View Code? Open in Web Editor NEW
17.0 2.0 6.0 71 KB

Go library for the Nokia Health API with full API coverage and documentation.

Home Page: https://godoc.org/github.com/jrmycanady/nokiahealth

License: MIT License

Go 100.00%
nokia health withings api golang go nokiahealth bpm thermo body

nokiahealth's Introduction

Go client for the Nokia Health API

This is a go client that allows easy access to the Nokia Health API and as of v2 supports the required Oauth2. More documentation regarding the Nokia Health API can be found here. More detailed documentation of the client can be found in the godocs.

v1 to v2 Changes

Nokia changed the API to allow Oauth2 while removing Oauth1 as an option. Due to this change, the client API has changed when it comes to handling authentication and tokens. For the most part the changes make things easier but they are breaking changes. The good new is there is no longer a dependency on the forked Ouath1 implementation.

Supported Resources

  • User Access Requests
  • Retrieving user body measurements
  • Retrieve activity measurements
  • Retrieve workouts
  • Retrieve intraday activities - Apparently requires additional authorization which I don't have yet so no testing.
  • Retrieve sleep measures - Limited testing so report any issues.
  • Retrieve sleep summary - Limited testing so report any issues.
  • Creating a notification
  • Retrieving a single notification
  • Retrieving all notifications for a user
  • Revoke notifications

Installation

go get github.com/jrmycanady/nokiahealth

Highlevel Usage Overview

It's best if you read up on Oauth2 if you are not familiar but the client should be simple enough to get working without understanding how Oauth2 works.

New User

  • Create a nokia account and register your app.
  • Create a client and perform the tasks to get the token/user struct.
    • Send user to Oauth2 authorization URL.
    • Catch the redirect back via a server or manually pulling the "code" from the path.
    • Generate a user.
  • Execute queries!

Returning User

  • Create client (Assuming you have an account otherwise the user couldn't be returning...)
  • Generate user from stored token.
  • Execute queries!

Highlevel Usage Example

New User

1. Obtain your ClientID and ClientSecret 2. Create a new client

clientID := "id"
clientSecrete := "secret"
clientRedirectURL := "url" // This is the URL nokia will redirect the client to 
                           // after they authorized your application. This is
                           // the same URL you provided when you registered your
                           // application with Nokia. 
                           // For any real world use you will need to have a
                           // http server running and listening on that URL
                           // so you can obtain the code that is retruned.
                           // For simple testing you can just manually navigate
                           // to the URL that will be generated and copy the
                           // code.

client := nokiahealth.NewClient(clientID, clientSecret, clientRedirectURL)

3. Generate the authorization URL.

authURL, _, err := client.AuthCodeURL() // Ignoring the state in this example
                                        // but in realworld use you will want
                                        // to record this to compare to the
                                        // state value returned by the redirect
                                        // from nokia to verify its a redirect
                                        // from your request. 
                                        // The stat is auto generated for you
                                        // using cyrto/rand but the random
                                        // generation can be replaced with your
                                        // own function if you would like.

4. User navigates to URL and the code and state are retrieved. 5. Create new user using returned code.

u, err := client.NewUserFromAuthCode(context.Background(), code) // Obviously
                                        // use whatever context you would like
                                        // here.
if err != nil {
    panic(err) // Handle the error however is appropriate for your code.
}

6. DONE - you now have a user that can make data requests.

Returning User

1. Create a new client

clientID := "id"
clientSecrete := "secret"
clientRedirectURL := "url" // This is the URL nokia will redirect the client to 
                           // after they authorized your application. This is
                           // the same URL you provided when you registered your
                           // application with Nokia. 
                           // For any real world use you will need to have a
                           // http server running and listening on that URL
                           // so you can obtain the code that is returned.
                           // For simple testing you can just manually navigate
                           // to the URL that will be generated and copy the
                           // code.

client := nokiahealth.NewClient(clientID, clientSecret, clientRedirectURL)

2. Generate a new user from the stored tokens.

// This creates a new user based on the tokens provided. Technically the 
// accessToken can be gibberish and the refresh Token is the one that is really
// required.
u, err := client.NewUserFromRefreshToken(context.Background(), accessToken, refreshToken)

3. DONE - you now have a user that can make data requests.

Making Requests

Requests are performed from methods on the User. Each request accepts a specific query struct with the details for the request. For example:

p := nokiahealth.BodyMeasuresQueryParams{}

t := time.Now().AddDate(0, 0, -14)
p.StartDate = &t

m, err := u.GetBodyMeasures(&p)

In most cases the response will contain all the information you need. Some methods provide additional optional processing that can provide a more usable form to the data. GetBodyMeasures is one of these methods. It's recommended to read the docs for each method to see how best to use them.

measures := m.ParseData()

Making context Requests

Every request method has a partner method ending with Ctx that takes a context that will be used for the HTTP call. This allows you to provide a custom context if you desire.

p := nokiahealth.BodyMeasuresQueryParams{}

t := time.Now().AddDate(0, 0, -14)
p.StartDate = &t

m, err := u.GetBodyMeasuresCtx(context.Background(), &p)

nokiahealth's People

Contributors

jrmycanady avatar pdbogen avatar ton1517 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

nokiahealth's Issues

Nokia Health is now Withings

Hullo!

Not sure how much you want to rename everything, but.. Nokia Health is back to being Withings. Importantly, several URLs have changed and the old URLs no longer work.

I've already opened golang/oauth2#375 to add a golang.org/x/oauth2/withings, and I have a branch that fixes the URLs within the app, renames files, adjusts strings, etc., but.... the package is still nokiahealth.

Make date field parsing optional.

Depending on the use case, dates may not need to be parsed. The query params should have an option to disable parsing. Also, a method should be provided that will parse the data when needed.

works in firefox but not chrome?

This is a quite bizarre problem, I think.. The code is working well for me in firefox, but in chrome the oauth process fails at at the call to (AccessRequest) GenerateUser. The error passed up from the library is oauth1: Response missing oauth_token or oauth_token_secret.

Here's my code: https://github.com/pdbogen/vator

And specifically here's the call that's failing: https://github.com/pdbogen/vator/blob/master/oauth.go#L45

I'll keep digging, myself, but if you have any insight or ideas, I'd welcome some clues.

Get lastUpdate

Jeremy awesome library, I just had a few questions:

  1. I'm getting an error message when trying to get the "LastUpdate", after setting the date to today or time.Now(). The specific Error message states: "Panic: runtime error: invalid memory address or nil pointer deference.
    code :

today := time.Now()
p := BodyMeasuresQueryParams{LastUpdate : &today}

The only time it works is when the time is set to "nil", in which case it returns all the weight data for the account.

  1. Is it possible to get the weight data (date, weight) in JSON {"data":"12/12/2012","weight":"77"} or get the original JSON results from Nokia ?

Run the go application

I'm new to go and i'm looking to make API calls to Nokia to fetch weight data from Nokia scale. I installed using "go get github.com/jrmycanady/nokiahealth" tried running it and its not working. Any idea why ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.