Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Fitbit, Unity, Android.

So I just finished making a tutorial that will work with Fitbit, Unity and Android. 

http://technicalartistry.blogspot.ca/2016/01/fitbit-unity-oauth-2-and-native.html

 

Hopefully soon I'll have my C# Unity FitbitAPI cleaned up and ready for people to use as well. 

Best Answer
10 REPLIES 10

Thank you for sharing!

Best Answer
0 Votes

No problem. I like to help where I can. 

Took me way to long when I started with my current project just to get OAuth related stuff done in Unity and if I can help make others not use weeks of research on it then I mid as well 😛

Best Answer
0 Votes

Hi, thank you for sharing this. 

 

I'm having a problem when I compile my Unity app for different platforms. 

 

When I run my app in the PC everythig work fine and I'm able to get fitbit data. However, when i run the app in a FreeBSD based platform it throws the error:

"errorType":"invalid_client","message":"Invalid authorization header format.

 

The problem is in this part of the code of the function GetProfileData()

var headers = new Dictionary<string, string>();  
headers["Authorization"] = "Bearer " + _oAuth2.Token;  
_wwwRequest = new WWW(_profileUrl, null, headers);   // returns the error only on FREEBSD based platform

The variable _wwwRequest above gets the following value:

{"errors":[{"errorType":"invalid_client","message":"Invalid authorization header format. Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process."}],"success":false}

 

The strange thing is that the following code on the UseReturnCode() function, where the app requests an access token, does works fine on the FREEBSD based platform, so it is able to connect to the WEB API:

_wwwRequest = new WWW(_tokenUrl, form.data, headers);

 

here the variable _wwwRequest contains the access token from the fitbit WebAPI (e.g. {"access_token":"asdfk3238123..." , "expires_in":28800, "refresh_token":"d1117bh...", ...

 

Any idea why the request for the profile data would not work on the FreeBSD platform?

What would could these errors mean for the FreeBSD base platform?

errors":[{"errorType":"invalid_client","message":"Invalid authorization header format

 

I have been stuck with this for a while and running out of ideas of how to solve this. Any help would be greatly appreciated.

Best Answer
0 Votes

@jorgearroyop That error means your header format is incorrect.

 

Authorization: Bearer access_token_here

 

I think you're missing the colon after "Authorization".

Andrew | Community Moderator, Fitbit

What motivates you?

Best Answer
0 Votes

That is distinctly possible.

It may be something I might have messed up in my code for the project. I'll check when I get home and we'll see who made the mistake haha

Best Answer
0 Votes

 

Hi @AndrewFitbit and @TEvashkevich, thanks for your quick response.

I tried adding the colon after "Authorization" but it still doesn't work. In fact, adding this makes the app not working even on Windows. 

 

I suspect the problem is not on the colon because I use a very similar piece of code to get the access token and it works on both Windows and the FreeBSD based platform. See below the code that works:

 

var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(_clientId + ":" + _consumerSecret);
var encoded = Convert.ToBase64String(plainTextBytes);
var form = new WWWForm();
var headers = form.headers;
headers["Authorization"] = "Basic " + encoded;
_wwwRequest = new WWW(_tokenUrl, form.data, headers);		// This request works without any complain about headers format

 

The problem I have is to access the profile data.

Best Answer
0 Votes

@jorgearroyop Can you show me the raw request you're making? 

Andrew | Community Moderator, Fitbit

What motivates you?

Best Answer
0 Votes

 

@AndrewFitbit I'm trying to figure out how can I get the raw request from the Unity WWW class. In the mean time I can provide you my header values. My header is declared in the following way:

var form = new WWWForm();
var headers = form.headers;

If I print the content of the header (type Dictionary<string, string>) when I unsuccessfully try to get the profile I get this:

Key = Content-Type, Value = application/x-www-form-urlencoded

Key = Authorization:, Value = Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIzTFNHSEwiLCJhd...

 

Do you think I need to specify a different content-type on the header? or should I encode the access token (_oAuth2.Token) i'm passing with the Bearer (see below)? 

 

headers["Authorization"] = "Bearer " + _oAuth2.Token; 

 

Best Answer
0 Votes

When I was dev'n this I was never able to come across a way to get the raw request (well you can but it's encrypted so doesn't really help you that much).

 

It's really strange that it works on Win but not another platform...it shouldn't make much difference unless that FreeBSD (I honestly don't know what exactly it is...) platform doesn't handle webRequests in the same way as Win/Mac.

 

Also, have you tried testing with my Unity Fitbit project to see if that works? Cause if that works then maybe it is indeed just a messup in your code somewhere.

https://github.com/TravisEvashkevich/UnityFitbit

Best Answer

Ok. So it seems that the error in the request response problem is solved, although I'm not entirely sure why and sounds like voodoo magic. Basically the problem was on the _profileUrl that was sent in the request. If the url has a "/" at the end it works on Windows but not on FREBSD. See below:

  

//private const string _profileUrl = "https://api.fitbit.com/1/user/-/profile.json/";  // Works on Windows but not on FREEBSD based platform
private const string _profileUrl = "https://api.fitbit.com/1/user/-/profile.json"; // Works on Windows and on FREEBSD based platform

 

Now I'm facing a different problem on my FREEBSD platform but I think I know what i will have to do. This error was also shown from the beginning but I didn't see how this error could influence the profile response I described before.

 

ExecutionEngineException: Attempting to JIT compile method 'System.Collections.Generic.GenericEqualityComparer`1<Newtonsoft.Json.Serialization.DefaultSerializationBinder/TypeNameKey>:.ctor ()' while running with --aot-only.

 

This problem is because Newtonsoft Json fails AOT in iOS and also at the FREEBSD based platform and also fails at deserializing:

http://stackoverflow.com/questions/16359628/json-net-under-unity3d-for-ios

 

Thanks @TEvashkevich and @AndrewFitbit for your comments and time.

Best Answer
0 Votes