0
 var appCredentials = new FitbitAppCredentials()
            {
                ClientId = "227RD5", //ConfigurationManager.AppSettings["FitbitClientId"],
                ClientSecret = "468c585ba98fc84e463952ca7a306c07" //ConfigurationManager.AppSettings["FitbitClientSecret"]
            };

            string UserId = Convert.ToBase64String((Encoding.ASCII.GetBytes("3J9685")));
            string code = Request.Params["code"];
            StringBuilder dataRequestUrl = new StringBuilder();
            dataRequestUrl.Append("https://api.fitbit.com/1/user/-");
            dataRequestUrl.Append("/profile.json");


            HttpWebRequest dataRequest = (HttpWebRequest)WebRequest.Create(dataRequestUrl.ToString());
            dataRequest.Method = "POST";

            string _auth = string.Format("{0}:{1}", appCredentials.ClientId, appCredentials.ClientSecret);
            var _encbyte = Encoding.ASCII.GetBytes(_auth);
            string _enc = Convert.ToBase64String(_encbyte);
            string _authorizationHeader = string.Format("{0} {1}", "Bearer", _enc);
            dataRequest.Headers["Authorization"] = _authorizationHeader;
            dataRequest.ContentType = "application/x-www-form-urlencoded";
            dataRequest.Accept = "application/json";

            string responseJson;
            HttpWebResponse response = null;
            try
            {
                response = dataRequest.GetResponse() as HttpWebResponse;
            }
            catch (WebException webEx)
            {
                response = webEx.Response as HttpWebResponse;
            }
            catch (Exception ex)
            {
                throw ex;
            }


            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                responseJson = reader.ReadToEnd();
            }

Above code give me unauthorized access error. Any one can find that what is the issue in code.I can authorize the user and refresh the user token but when I will try to get userprofile.json call it will give me unauthorized access error.

Matt
  • 14,906
  • 27
  • 99
  • 149
  • use following link http://stackoverflow.com/questions/37520816/how-to-integrate-fitbit-api-in-ios-app-using-swift/39697291#39697291 – Mahendra Y Sep 26 '16 at 07:52

1 Answers1

0

For getting user profile information, we need only access-token sent as part of header.

You don't need clientId and ClientSecret for getting profile information.

Change your code like this:

        StringBuilder dataRequestUrl = new StringBuilder();
        dataRequestUrl.Append("https://api.fitbit.com/1/user/-/profile.json");

        HttpWebRequest dataRequest = (HttpWebRequest)WebRequest.Create(dataRequestUrl.ToString());

        String accessToken = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiXXXXXXXXXX";
        dataRequest.Method = "GET";
        dataRequest.Headers.Add("Authorization","Bearer "+ accessToken);
        dataRequest.ContentType = "application/json";

        string responseJson;
        HttpWebResponse response = null;
        try
        {
            response = dataRequest.GetResponse() as HttpWebResponse;
        }
        catch (WebException webEx)
        {
            response = webEx.Response as HttpWebResponse;
        }
        catch (Exception ex)
        {
            throw ex;
        }

        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            responseJson = reader.ReadToEnd();
        }
Manav
  • 160
  • 8