Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A super-simple sample? #182

Open
Moskus opened this issue Jul 30, 2016 · 13 comments
Open

A super-simple sample? #182

Moskus opened this issue Jul 30, 2016 · 13 comments

Comments

@Moskus
Copy link

Moskus commented Jul 30, 2016

I have a hard time wrapping my head around the samples in the new version. I have a hard time just compiling them in my instance of VS 2015, there are references missing and I don't have SQL server 2012 either (and I don't plan to use it). The old console application sample was excellent, and was exactly what I was looking for, I'm writing a plugin for home automation software HomeSeer, and the plugins are essentially console applications with a web front end.

Is it possible for somebody to create a (extremely) simple asp.net project? Just a page or two demonstrating how to authenticate using OAuth2.0, and just write some data (Response.Write() is fine!).

@WestDiscGolf
Copy link
Contributor

Hi Moskus,

Sorry you've been experiencing issues with compiling the source. Can you let us know what the issue is when you try? I have rebased the latest "portable-async" branch into my fork and it builds ok for me. Have you made sure you have restored the correct nuget packages?

I put together a very super simple example of the the new library in a console application and you can run it below ...

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Fitbit.Api.Portable;
using Fitbit.Api.Portable.OAuth2;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Task.Run(async () =>
            {
                // credentials
                FitbitAppCredentials credentials = new FitbitAppCredentials()
                {
                    ClientId = "<insert your client id>",
                    ClientSecret = "<insert your client secret>"
                };

                // authenticate
                var helper = new OAuth2Helper(credentials, "http://localhost:1234/fitbit/callback"); // example call back url
                var authUrl = helper.GenerateAuthUrl(new[] { "activity" });

                Process.Start(authUrl);

                var pin = Console.ReadLine();

                var token = await helper.ExchangeAuthCodeForAccessTokenAsync(pin);

                // var request
                var fitbitClient = new FitbitClient(credentials, token);

                var stats = await fitbitClient.GetActivitiesStatsAsync();

                Console.WriteLine($"Total steps: {stats.Lifetime.Total.Steps}");
                Console.ReadLine();

            }).Wait();
        }
    }
}

This will fire up, spin up a browser window where you can authenticate, then you will be redirected back to the url (which won't resolve anywhere as we're in a console app) however it will look something like this ...

http://localhost:1234/fitbit/callback?code=<returned code here>#=`

... and you take the code (minus #_=#) and paste it into the console application.

Note: make sure the callback url is exactly the same as the one configured in the Fitbit application settings in the dev area as they are (or at least were) case sensitive.

This application is currently running with the following nuget packages configuration ...

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Fitbit.NET" version="2.0.1-RC2" targetFramework="net461" />
  <package id="Microsoft.Bcl" version="1.1.9" targetFramework="net461" />
  <package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net461" />
  <package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net461" />
  <package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net461" />
  <package id="Newtonsoft.Json" version="8.0.2" targetFramework="net461" />
</packages>

Hope this helps!

@fireole
Copy link

fireole commented Jul 30, 2016

Ah, excellent example. Perfect!!

And thanks for what you are doing with the project!

On Sat, Jul 30, 2016 at 11:07 AM, Adam Storr [email protected]
wrote:

Hi Moskus,

Sorry you've been experiencing issues with compiling the source. Can you
let us know what the issue is when you try? I have rebased the latest
"portable-async" branch into my fork and it builds ok for me. Have you made
sure you have restored the correct nuget packages?

I put together a very super simple example of the the new library in a
console application and you can run it below ...

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Fitbit.Api.Portable;
using Fitbit.Api.Portable.OAuth2;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Task.Run(async () =>
{
// credentials
FitbitAppCredentials credentials = new FitbitAppCredentials()
{
ClientId = "",
ClientSecret = ""
};

            // authenticate
            var helper = new OAuth2Helper(credentials, "http://localhost:1234/fitbit/callback"); // example call back url
            var authUrl = helper.GenerateAuthUrl(new[] { "activity" });

            Process.Start(authUrl);

            var pin = Console.ReadLine();

            var token = await helper.ExchangeAuthCodeForAccessTokenAsync(pin);

            // var request
            var fitbitClient = new FitbitClient(credentials, token);

            var stats = await fitbitClient.GetActivitiesStatsAsync();

            Console.WriteLine($"Total steps: {stats.Lifetime.Total.Steps}");
            Console.ReadLine();

        }).Wait();
    }
}

}

This will fire up, spin up a browser window where you can authenticate,
then you will be redirected back to the url (which won't resolve anywhere
as we're in a console app) however it will look something like this ...

http://localhost:1234/fitbit/callback?code=<returned` code here>#=

... and you take the code (minus #_=#) and paste it into the console
application.

Note: make sure the callback url is exactly the same as the one configured
in the Fitbit application settings in the dev area as they are (or at least
were) case sensitive.

This application is currently running with the following nuget packages
configuration ...

Hope this helps!


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#182 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AA8ID8OltprHzQb_fU5heMDbzWqQ9MPSks5qa5L7gaJpZM4JYv-m
.

Antonio Olander

@Moskus
Copy link
Author

Moskus commented Aug 2, 2016

@WestDiscGolf , thank you so much for this sample! It's seems to be EXACTLY what I need! I'll try it tonight! :)
I apologize for the late reply, apparently I had messed up my notification email setup.

I'll grab all the errors from the latest "portable-async" branch so you can have a look. :)

@Moskus
Copy link
Author

Moskus commented Aug 2, 2016

I just did a quick test. Some of the packages are newer (e.g. RC3 apparently happened), and I had to use .NET Framework 4.6.1, but now it compiles. :)

However, I get this error:
error

I've tried to register a new app with "http://localhost" as the callback URI just to check, but still nothing.

EDIT: Okay, I learned that the callback URI needs to be exactly what's entered in the Fitbit app. That's going to be a problem when I don't know what it is! The users use their own web server (HomeSeer provides it), and I don't know the URIs... :(

Any pointers?

... and can I just store the token?
EDIT 2: Yes I can! :)
EDIT 3: Or not? How long does a token last?
EDIT 4: sigh I changed the scope types, and had to reset the token. But is it possible to set the validation time?

@Moskus
Copy link
Author

Moskus commented Aug 4, 2016

How can I refresh a token? I'm trying
var newToken = await client.RefreshOAuth2TokenAsync()
... but it fails miserably every time.

@jwisener
Copy link

so what is the offical source for this project? Having a really tough time getting any forked to pull down and compile.

@WestDiscGolf
Copy link
Contributor

The default branch, async-portable, is the main branch.

@jwisener
Copy link

jwisener commented Nov 21, 2016

Thanks, It's when I'm opening the the Fitbit-WithSamples.sln that I'm having some issues. 1. The Fitbit.Common project says, 'unavailable' (I can remove it). But then the SampleWebMVC.Portable project cannot seem to restore any of the nuget packages.

@WestDiscGolf
Copy link
Contributor

I've not personally tried all the slns and I don't think they've all been kept up to date. If you open the Fitbit.sln it will have the library project and the tests project.

Common doesn't exist anymore which is why I believe the slns haven't been kept up to date

@AlexGhiondea
Copy link
Contributor

I just saw this issue -- I submitted a PR with a very similar console app: #218

In addition, it automates the retrieval of the code form the redirect url.

@pedjaaaa
Copy link

Hi, is there any way to make entering of the pin automatic? Can we somehow bypass that manual entry?

@AlexGhiondea
Copy link
Contributor

@pedjaaaa yes -- I am doing that here:
https://github.com/aarondcoleman/Fitbit.NET/pull/218/files#diff-661b127c81dfffaa0d3fd63c692050e3

The basic idea is that you are going to redirect from the fitbit authorization to http://localhost: and there will be an HttpListener listening at http://localhost: and that will retrieve the code automatically.

The only downside is that it will require that the app run as admin otherwise you can't listen using the HttpListener -- however, this is only required the first time you authorize the app (and the sample I linked to will automatically re-launch itself as admin when needed).

@pedjaaaa
Copy link

pedjaaaa commented Feb 15, 2019

Hi @AlexGhiondea,

Thanks for your help :)
Had a problem with localhost, fixed it, thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants