Exemplo n.º 1
0
        public static bool UpdateLocation(HttpContext context, Location location, Uri callback)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (location == null)
            {
                throw new ArgumentNullException("location");
            }

            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            // Update the user's location
            var request = OAuthRequest.Create(
                new EndPoint("https://fireeagle.yahooapis.com/api/0.1/update", "POST"),
                FireEagle.OAuthService,
                callback,
                HttpContext.Current.Session.SessionID);

            request.VerificationHandler = AspNetOAuthRequest.HandleVerification;

            // Send the location latitude and longitude with the request
            OAuthResponse response = request.GetResource(
                new NameValueCollection()
            {
                { "lat", location.Point.Latitude.ToString(CultureInfo.InvariantCulture) },
                { "lon", location.Point.Longitude.ToString(CultureInfo.InvariantCulture) }
            });

            if (!response.HasProtectedResource)
            {
                throw new AuthorizationRequiredException()
                      {
                          AuthorizationUri = FireEagle.OAuthService.BuildAuthorizationUrl(response.Token)
                      }
            }
            ;

            // Store the access token
            context.Session["access_token"] = response.Token;

            // Load the response XML
            XmlDocument responseXml = new XmlDocument();

            responseXml.Load(response.ProtectedResource.GetResponseStream());

            // Check the response status
            return(responseXml.SelectSingleNode("rsp/@stat").Value == "ok");
        }
    }
Exemplo n.º 2
0
        public static Location GetLocation(HttpContext context, Uri callback)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            // Find the user's location
            var request = OAuthRequest.Create(
                new EndPoint("https://fireeagle.yahooapis.com/api/0.1/user", "GET"),
                FireEagle.OAuthService,
                callback,
                HttpContext.Current.Session.SessionID);

            request.VerificationHandler = AspNetOAuthRequest.HandleVerification;

            OAuthResponse response = request.GetResource();

            if (!response.HasProtectedResource)
            {
                throw new AuthorizationRequiredException()
                      {
                          AuthorizationUri = FireEagle.OAuthService.BuildAuthorizationUrl(response.Token)
                      }
            }
            ;

            // Store the access token
            context.Session["access_token"] = response.Token;

            // Load the response XML
            XmlDocument responseXml = new XmlDocument();

            responseXml.Load(response.ProtectedResource.GetResponseStream());

            // Check the response status
            if (responseXml.SelectSingleNode("rsp/@stat").Value == "ok")
            {
                return(Location.Parse(responseXml.SelectSingleNode("rsp/user/location-hierarchy/location[@best-guess='true']")));
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 3
0
        public string Access(string url)
        {
            // Create OAuthService object, containing oauth consumer configuration
            service = OAuthService.Create(
                new EndPoint(RequestTokenUrl, "POST"),         // requestTokenEndPoint
                new Uri(AuthorizationUrl),                     // authorizationUri
                new EndPoint(AccessTokenUrl, "POST"),          // accessTokenEndPoint
                true,                                          // useAuthorizationHeader
                "http://wbsapi.withings.net",                  // realm
                "HMAC-SHA1",                                   // signatureMethod
                "1.0",                                         // oauthVersion
                new OAuthConsumer(ConsumerKey, ConsumerSecret) // consumer
                );
            string content;

            try
            {
                // Create OAuthRequest object, providing protected resource URL

                OAuthRequest request;
                if (m_bAuthorized)
                {
                    request = OAuthRequest.Create(
                        new EndPoint(url, "GET"),
                        service,
                        callbackurl,
                        requestToken, accessToken);
                }
                else
                {
                    request = OAuthRequest.Create(
                        new EndPoint(url, "GET"),
                        service,
                        callbackurl,
                        sessionId);
                }


                // Assign verification handler delegate
                request.VerificationHandler = AspNetOAuthRequest.HandleVerification;

                // Call OAuthRequest object GetResource method, which returns OAuthResponse object
                OAuthResponse response = request.GetResource();

                // Check if OAuthResponse object has protected resource
                if (!response.HasProtectedResource)
                {
                    m_bAuthorized = false;
                    // If not we are not authorized yet, build authorization URL and redirect to it
                    string authorizationUrl = service.BuildAuthorizationUrl(response.Token).AbsoluteUri;
                    return(authorizationUrl);
                }
                else
                {
                    //Save our data
                    requestToken = request.RequestToken;
                    accessToken  = request.AccessToken;
                }

                // Store the access token in session variable
                access_token = response.Token;


                // Initialize the XmlDocument object and OAuthResponse object's protected resource to it
                m_bAuthorized = true;


                Stream ms = response.ProtectedResource.GetResponseStream();
                // Jump to the start position of the stream
                ms.Seek(0, SeekOrigin.Begin);

                StreamReader rdr            = new StreamReader(ms);
                string       anwser_content = rdr.ReadToEnd();


                return(anwser_content);
            }

            catch (OAuthRequestException ex)
            {
                return(ex.Message);
            }
        }