Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fkberthold · Jul 03, 2014 at 07:28 PM · c#www

How do I request an oauth token in FitBit with WWW

I am trying to connect to FitBit using the WWW class in C#. When I try to get an oauth token, it fails with a "401 Unauthorized" message. I've tried directly connecting via curl with, as far as I can tell, the same headers and it's successful. Can anyone spot what I'm doing wrong?

When I enter this at the command line:

 curl -i -X 'POST' 'https://api.fitbit.com/oauth/request_token' -H 'Authorization: OAuth oauth_consumer_key="9e2fe319f5154a658731f73ce8373ef6", oauth_nonce="47F7793F", oauth_signature="u4Mvy%2BfR5rk8C%2BXERSz%2Bc%2F9ppj0%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1404279962", oauth_version="1.0"'

I Get:

 HTTP/1.1 200 OK
 Server: nginx
 Date: Wed, 02 Jul 2014 05:47:57 GMT
 Content-Type: application/x-www-form-urlencoded;charset=UTF-8
 Content-Length: 126
 Connection: keep-alive
 X-UA-Compatible: IE=edge,chrome=1
 Set-Cookie: fhttps=""; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/
 Expires: Thu, 01 Jan 1970 00:00:00 GMT
 Cache-control: no-store, no-cache, must-revalidate
 Pragma: no-cache
 Content-Language: en-US
 X-Frame-Options: SAMEORIGIN
 
 oauth_token=5c87cb5625ad6afdac957c7460d5c1d3&oauth_token_secret=73960a8e22f3c7c2cfb258fd8f83221e&oauth_callback_confirmed=true%                                                              


Using this code in Unity:

 using UnityEngine;
 using System;
 using System.Collections;
 using System.Text;
 using System.Text.RegularExpressions;
 
 public class Demo : MonoBehaviour 
 {
 
     public class RequestTokenResponse
     {
         public string Token { get; set; }
         public string TokenSecret { get; set; }
     }
 
     // You need to register your game or application in Fitbit to get cosumer key and secret.
     // Go to this page for registration: https://dev.fitbit.com/apps/new 
     public string CONSUMER_KEY = "9e2fe319f5154a658731f73ce8373ef6";
     public string CONSUMER_SECRET = "75f4e6d41d5f4469a49e8b929dd0b6b4";
     private string RequestTokenURL = "http://api.fitbit.com/oauth/request_token";
 
     // Use this for initialization
     IEnumerator Start() 
     {
         byte[] dummy = new byte[1];
         dummy[0] = 0;
 
         String timestamp = "1404279962";
         String signature = "u4Mvy+fR5rk8C+XERSz+c/9ppj0=";
 
         StringBuilder parameters = new StringBuilder();
         parameters.AppendFormat("OAuth oauth_consumer_key=\"{0}\",oauth_nonce=\"{1}\",oauth_signature=\"{2}\"," + 
                                        "oauth_signature_method=\"{3}\",oauth_timestamp=\"{4}\",oauth_version=\"{5}\"", 
                                        "9e2fe319f5154a658731f73ce8373ef6",
                                        "47F7793F",
                                         UrlEncode(signature),
                                        "HMAC-SHA1",
                                        timestamp,
                                        "1.0");
 
         Hashtable headers = new Hashtable();
         headers["Authorization"] = parameters.ToString();
         Debug.Log ("Authorization Header: " + headers["Authorization"]);
 
 
         WWW web = new WWW(RequestTokenURL, dummy, headers);
 
         yield return web;
 
         if (!string.IsNullOrEmpty(web.error))
         {
             Debug.Log(string.Format("GetRequestToken - failed. error : {0}", web.error));
         }
         else
         {
             String Token = Regex.Match(web.text, @"oauth_token=([^&]+)").Groups[1].Value;
             String TokenSecret = Regex.Match(web.text, @"oauth_token_secret=([^&]+)").Groups[1].Value;
             Debug.Log (string.Format ("Token: {0} TokenSecret: {1}", Token, TokenSecret));
         }
     }
         
     private static string UrlEncode(string value)
     {
         if (string.IsNullOrEmpty(value))
         {
             return string.Empty;
         }
             
         value = Uri.EscapeDataString(value);
             
         // UrlEncode escapes with lowercase characters (e.g. %2f) but oAuth needs %2F
         value = Regex.Replace(value, "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper());
             
         // these characters are not escaped by UrlEncode() but needed to be escaped
         value = value
             .Replace("(", "%28")
                 .Replace(")", "%29")
                 .Replace("$", "%24")
                 .Replace("!", "%21")
                 .Replace("*", "%2A")
                 .Replace("'", "%27");
         
         // these characters are escaped by UrlEncode() but will fail if unescaped!
         value = value.Replace("%7E", "~");
         
         return value;
     }
     
     // Update is called once per frame
     void Update() 
     {
     }
 }


When I execute it I get:

 Authorization Header: OAuth oauth_consumer_key="9e2fe319f5154a658731f73ce8373ef6",oauth_nonce="47F7793F",oauth_signature="u4Mvy%2BfR5rk8C%2BXERSz%2Bc%2F9ppj0%3D",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1404279962",oauth_version="1.0"
 UnityEngine.Debug:Log(Object)
 <Start>c__Iterator0:MoveNext() (at Assets/Demo.cs:44)
 
 GetRequestToken - failed. error : 401 Unauthorized
 UnityEngine.Debug:Log(Object)
 <Start>c__Iterator0:MoveNext() (at Assets/Demo.cs:53)




Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Graham-Dunnett ♦♦ · Jul 03, 2014 at 07:29 PM 0
Share

There's a million typos in the script you sent. You had some search and replace nightmares?

avatar image fkberthold · Jul 03, 2014 at 08:04 PM 0
Share

@Graham Dunnett: Thank you for the catch, I'd copy/pasted it between three places (one of them Vim) and it must have gotten corrupted in the process. I've fixed it.

avatar image Graham-Dunnett ♦♦ · Jul 21, 2014 at 10:26 AM 0
Share

http://forum.unity3d.com/threads/how-do-i-request-an-oauth-token-in-fitbit-with-www.258034/

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TEvashkevich · Jul 09, 2015 at 06:54 AM

I figured out how to get Fitbit and Unity to work together. I made a post breaking it down with the applicable Unity C# code to get your first set of data back from Fitbit.

The only thing that is up to you is which Webview you use to get the initial code back from your first call.

http://technicalartistry.blogspot.nl/2015/07/oauth2-unity-and-month-of-cursing.html

If you're reading this and wanting to do it for Fitbit still, Check this new blogpost as you can no longer use Webviews as I was using in my last post. This post covers how to do OAuth in Android without the use of a Webview in Android (FREE as well)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TEvashkevich · Jan 13, 2016 at 09:18 AM 0
Share

New Blogpost! This blogpost now covers doing OAuth for Fitbit on Android with Unity WITHOUT the need for a webview (since this is now prohibited by Fitbit). Hope it helps people :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Download images from server and storing it in Resources. 1 Answer

Yield return request never returns 0 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges