1 Replies - 1124 Views - Last Post: 29 July 2013 - 08:57 AM Rate Topic: -----

#1 y_chen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-July 13

Automate OAuth2 authentication

Posted 25 July 2013 - 12:46 AM

I'm working with measuring vital signs devices. The manufacturer is iHealth. These connect with a smartphone where is installed an app that when you are logged in with your account it sends the measurements to their server. I have to connect to this server and extract the datas from many accounts but at the moment I will be satisfy to extract only mine. To do this iHealth provides a Sandbox like example that you can download at this url: Attached File  SandboxAPISample.zip (81.81K)
Number of downloads: 23 .

In this example all the needed variables (client_id, client_secret, redirect_url etc) are set up in the ConnectToiHealthlabs.cs class.

When you start the project the page GetStarted.aspx is loaded and only a button appears. By clicking the button the follow method is called from ConnectToiHealthlabs.cs class:

public void GetCode()
    {
        string url = url_authorization
            + "?client_id=" + client_id
            + "&response_type=" + response_type_code
            + "&redirect_uri=" + redirect_uri
            + "&APIName=" + APIName_BP + " " + APIName_Weight;

        HttpContext.Current.Response.Redirect(url);
    }


The url where it redirects is
http://sandboxapi.ihealthlabs.com/api/OAuthv2/userauthorization.ashx?client_id=e4dce2f7027044e0a6ce82ef44d27e23&response_type=code&redirect_uri=http://localhost:9201/TestPage.aspx&APIName=OpenApiBP OpenApiWeight


So you will arrive to the page
http://sandboxapi.ihealthlabs.com/api/OAuthv2/oauthlogin.aspx?redirect_uri=%2fapi%2fOAuthv2%2fuserauthorization.ashx%3fclient_id%3de4dce2f7027044e0a6ce82ef44d27e23%26response_type%3dcode%26redirect_uri%3dhttp%3a%2f%2flocalhost%3a9201%2fTestPage.aspx%26APIName%3dOpenApiBP%2520OpenApiWeight


Now you can authenticate with the sandbox credentials:

username: sandboxuser@ihealthlabs.com

Password: 111111

If the credentials are right you will be redirected to TestPage.aspx where this method is called:

public bool GetAccessToken(string code, string client_para, HttpContext httpContext)
    {
        string url = url_authorization
        + "?client_id=" + client_id
        + "&client_secret=" + client_secret
        + "&client_para=" + client_para
        + "&code=" + code
        + "&grant_type=" + grant_type_authorization_code
        + "&redirect_uri=" + redirect_uri;

        string ResultString = this.HttpGet(url);

        if (ResultString.StartsWith("{\"Error\":"))
        {
            this.Error = JsonDeserializ<ApiErrorEntity>(ResultString);
            return false; 
        }
        else
        {
            AccessTokenEntity accessToken = this.JsonDeserializ<AccessTokenEntity>(ResultString);
            httpContext.Session["token"] = accessToken;
            return true;
        }
    }


The result url is
http://sandboxapi.ihealthlabs.com/api/OAuthv2/userauthorization.ashx?client_id=e4dce2f7027044e0a6ce82ef44d27e23&client_secret=bb6a0326db55468f8f474ad5fd79aa0a&client_para=123&code=a1yBBCZu80DXlhnSmyYEKIY6cQ73cVj7O9i1hv7rH68EQIgIlY0*pODnNbD2gyEMu6UnHvksB5Ndf42-I8i4xPHb4jTqzpue0S9PVLTCjw2bUMOZCyiBoCBjcllUzHzo&grant_type=authorization_code&redirect_uri=http://localhost:9201/TestPage.aspx


So if everything is correct you can see a button to download BP data (url:
http://sandboxapi.ihealthlabs.com/api/OpenApi/downloadbpdata.ashx?access_token=a1yBBCZu80DXlhnSmyYEKIY6cQ73cVj7O9i1hv7rH6-zMHK6iEu9OCg4MCErg-lOvm0WKcGwfZfVWanKB3-s2BLUZniYfwn99tsdWTRhlvBU8ZFTcIk-9qPeMtN*zVTNosPp4RsCiHF2D-o-DvpdwjS9qQhcwMEKgunaLSK1Qu8&client_id=e4dce2f7027044e0a6ce82ef44d27e23&client_secret=bb6a0326db55468f8f474ad5fd79aa0a&redirect_uri=http://localhost:9201/TestPage.aspx&sc=082a65ac25db4262b795f635c974de47&sv=bd82a25dcf18446b90f3219ef7d0b441
),

download weight data (url:
http://sandboxapi.ihealthlabs.com/api/OpenApi/downloadweightdata.ashx?access_token=a1yBBCZu80DXlhnSmyYEKIY6cQ73cVj7O9i1hv7rH6-zMHK6iEu9OCg4MCErg-lOvm0WKcGwfZfVWanKB3-s2BKc5tIrfqZpz5gC*IAhOnIr1J1PhT0M0*IPDtIZ6SLuTZtNC5Hn0C4u0yDBo9zunmoDwkoz2wKmAqE4aXUJFE4&client_id=e4dce2f7027044e0a6ce82ef44d27e23&client_secret=bb6a0326db55468f8f474ad5fd79aa0a&redirect_uri=http://localhost:9201/TestPage.aspx&sc=082a65ac25db4262b795f635c974de47&sv=add22354420244ba9e0f3a5a6b402096
)

and refresh token (url:
http://sandboxapi.ihealthlabs.com/api/OAuthv2/userauthorization.ashx?client_id=e4dce2f7027044e0a6ce82ef44d27e23&client_secret=bb6a0326db55468f8f474ad5fd79aa0a&client_para=&refresh_token=a1yBBCZu80DXlhnSmyYEKIY6cQ73cVj7O9i1hv7rH6-zMHK6iEu9OCg4MCErg-lOvm0WKcGwfZfVWanKB3-s2Pl0dL9j3ijv6zPwlqNtRkO2lRuNbVszrIeKHqx2ZToJCLvFHNHmMJWni*QMiIgWRl-B3VIHGdpGFPpD5iD0p9Y&response_type=refresh_token&redirect_uri=http://localhost:9201/TestPage.aspx
).

In this example it databinds a repeater with the list that returns DownloadBPData and DownloadWeightData methods to show the values in the page.

My goal is to arrive these two lists without open popups and without manual authentications scheduling the update speed.

All in a log like page:

24/07/2013 12:00 - Found new data for user ABC

24/07/2013 11:30 - Found new data for user XYZ

24/07/2013 11:00 - etc...

But I really don't know where to start.

The only documentation that I have is the following file: Attached File  Open API Protocol.zip (459.46K)
Number of downloads: 56

The url in the sandbox project are differents from the server where I have to connect I found a lot of OAuth2 libraries but all the examples are for Facebook, Twitter etc.

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Automate OAuth2 authentication

#2 y_chen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-July 13

Re: Automate OAuth2 authentication

Posted 29 July 2013 - 08:57 AM

No one can help me?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1