//Final step. Take this authorization information and use it in the app public ActionResult Callback() { RequestToken token = new RequestToken(); token.Token = Request.Params["oauth_token"]; token.Secret = Session["FTGRequestTokenSecret"].ToString(); token.Verifier = Request.Params["oauth_verifier"]; string ConsumerKey = ConfigurationManager.AppSettings["FTGConsumerKey"]; string ConsumerSecret = ConfigurationManager.AppSettings["FTGConsumerSecret"]; //this is going to go back to Fitbit one last time (server to server) and get the user's permanent auth credentials //create the Authenticator object Authenticator authenticator = new Authenticator(ConsumerKey, ConsumerSecret, "http://api.42goals.com/v1/oauth/request_token/", "http://api.42goals.com/v1/oauth/access_token/", "http://42goals.com/settings/authorize/"); //execute the Authenticator request to Fitbit AuthCredential credential = authenticator.ProcessApprovedAuthCallback(token); //here, we now have everything we need for the future to go back to Fitbit's API (STORE THESE): // credential.AuthToken; // credential.AuthTokenSecret; // credential.UserId; // For demo, put this in the session managed by ASP.NET Session["FTGAuthToken"] = credential.AuthToken; Session["FTGAuthTokenSecret"] = credential.AuthTokenSecret; Session["FTGUserId"] = credential.UserId; return RedirectToAction("Index", "Home"); }
public string GenerateAuthUrlFromRequestToken(RequestToken token) { RestRequest request = null; request = new RestRequest(token.Token + "/"); var authclient = new RestClient(AuthorizeUrl); var url = authclient.BuildUri(request).ToString(); return url; }
/// <summary> /// For Desktop authentication. Your code should direct the user to the FitBit website to get /// Their pin, they can then enter it here. /// </summary> /// <param name="pin"></param> /// <returns></returns> public AuthCredential GetAuthCredentialFromPin(string pin, RequestToken token) { var request = new RestRequest("oauth/access_token", Method.POST); client.Authenticator = OAuth1Authenticator.ForAccessToken(ConsumerKey, ConsumerSecret, token.Token, token.Secret, pin); var response = client.Execute(request); var qs = RestSharp.Contrib.HttpUtility.ParseQueryString(response.Content); return new AuthCredential() { AuthToken = qs["oauth_token"], AuthTokenSecret = qs["oauth_token_secret"], UserId = qs["encoded_user_id"] }; }
/// <summary> /// First step in the OAuth process is to ask Fitbit for a temporary request token. /// From this you should store the RequestToken returned for later processing the auth token. /// </summary> /// <returns></returns> public RequestToken GetRequestToken() { client.Authenticator = OAuth1Authenticator.ForRequestToken(this.ConsumerKey, this.ConsumerSecret); var request = new RestRequest(RequestTokenUrl, Method.GET); var response = client.Execute(request); var qs = HttpUtility.ParseQueryString(response.Content); RequestToken token = new RequestToken(); token.Token = qs["oauth_token"]; token.Secret = qs["oauth_token_secret"]; if (response.StatusCode != System.Net.HttpStatusCode.OK) throw new Exception("Request Token Step Failed"); return token; }
public AuthCredential ProcessApprovedAuthCallback(RequestToken token) { if (string.IsNullOrWhiteSpace(token.Token)) throw new Exception("RequestToken.Token must not be null"); //else if client.Authenticator = OAuth1Authenticator.ForRequestToken(this.ConsumerKey, this.ConsumerSecret); var request = new RestRequest(AccessTokenUrl, Method.POST); client.Authenticator = OAuth1Authenticator.ForAccessToken( this.ConsumerKey, this.ConsumerSecret, token.Token, token.Secret, token.Verifier ); var response = client.Execute(request); //Assert.NotNull(response); //Assert.Equal(HttpStatusCode.OK, response.StatusCode); if (response.StatusCode != HttpStatusCode.OK) throw new Exception(response.Content); var qs = HttpUtility.ParseQueryString(response.Content); //not actually parsing querystring, but body is formatted like htat var oauth_token = qs["oauth_token"]; var oauth_token_secret = qs["oauth_token_secret"]; var encoded_user_id = qs["encoded_user_id"]; //Assert.NotNull(oauth_token); //Assert.NotNull(oauth_token_secret); /* request = new RestRequest("account/verify_credentials.xml"); client.Authenticator = OAuth1Authenticator.ForProtectedResource( this.ConsumerKey, this.ConsumerSecret, oauth_token, oauth_token_secret ); response = client.Execute(request); */ return new AuthCredential() { AuthToken = oauth_token, AuthTokenSecret = oauth_token_secret, UserId = encoded_user_id }; //Assert.NotNull(response); //Assert.Equal(HttpStatusCode.OK, response.StatusCode); //request = new RestRequest("statuses/update.json", Method.POST); //request.AddParameter("status", "Hello world! " + DateTime.Now.Ticks.ToString()); //client.Authenticator = OAuth1Authenticator.ForProtectedResource( // consumerKey, consumerSecret, oauth_token, oauth_token_secret //); //response = client.Execute(request); //Assert.NotNull(response); //Assert.Equal(HttpStatusCode.OK, response.StatusCode); }