コード例 #1
0
 public BandController()
 {
     _appCredentials = new BandAppCredentials
     {
         ClientId     = ConfigurationManager.AppSettings["BandClientId"],
         ClientSecret = ConfigurationManager.AppSettings["BandClientSecret"]
     };
 }
コード例 #2
0
        public HttpResponseMessage Authorize()
        {
            var appCredentials = new BandAppCredentials()
            {
                ClientId     = ConfigurationManager.AppSettings["BandClientId"],
                ClientSecret = ConfigurationManager.AppSettings["BandClientSecret"]
            };

            _credentials = appCredentials;

            //Provide the App Credentials. You get those by registering your app at dev.fitbit.com
            //Configure Fitbit authenticaiton request to perform a callback to this constructor's Callback method
            //var authenticator = new OAuth2Helper(appCredentials, Request.Url.GetLeftPart(UriPartial.Authority) + "/band/callback");
            var authenticator = new OAuth2Helper(appCredentials, ConfigurationManager.AppSettings["BaseUrl"] + "/api/band/callback");

            string[] scopes = new string[] { "profile" };

            string authUrl = authenticator.GenerateAuthUrl(scopes, null);

            using (var client = new HttpClient(new HttpClientHandler {
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
            }))
            {
                client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("PostmanRuntime", "7.3.0"));
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));

                HttpResponseMessage response = client.GetAsync(authUrl).Result;
                if (response.IsSuccessStatusCode)
                {
                    return(response);
                }

                return(response);

                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }
        }
コード例 #3
0
        public async Task <HttpResponseMessage> Callback(string code)
        {
            var appCredentials = new BandAppCredentials()
            {
                ClientId     = ConfigurationManager.AppSettings["BandClientId"],
                ClientSecret = ConfigurationManager.AppSettings["BandClientSecret"]
            };

            var authenticator = new OAuth2Helper(appCredentials, ConfigurationManager.AppSettings["BaseUrl"] + "/api/Band/Callback");

            OAuth2AccessToken accessToken = await authenticator.ExchangeAuthCodeForAccessTokenAsync(code);

            if (!string.IsNullOrEmpty(accessToken.Token))
            {
                //Store credentials in FitbitClient. The client in its default implementation manages the Refresh process
                //var fitbitClient = GetBandClient(accessToken);
                var resp = new HttpResponseMessage(HttpStatusCode.OK);
                resp.Content = new StringContent(JsonConvert.SerializeObject(accessToken), Encoding.UTF8);

                return(new HttpResponseMessage(HttpStatusCode.OK));
            }

            return(new HttpResponseMessage(HttpStatusCode.BadRequest));
        }
コード例 #4
0
 public OAuth2Helper(BandAppCredentials credentials, string redirectUri)
 {
     _clientId     = credentials.ClientId;
     _clientSecret = credentials.ClientSecret;
     _redirectUri  = redirectUri;
 }