Fitbit Oauth2.0 API - 获取用户个人资料

时间:2016-08-29 14:12:06

标签: oauth-2.0 fitbit

 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();
            }

以上代码会给我未经授权的访问错误。任何人都可以找到代码中的问题。我可以授权用户并刷新用户令牌,但是当我尝试获取userprofile.json时,它会给我带来未经授权的访问错误。

1 个答案:

答案 0 :(得分:0)

要获取用户个人资料信息,我们只需要作为标题的一部分发送的访问令牌。

您不需要clientId和ClientSecret来获取个人资料信息。

更改您的代码:

        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();
        }