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;
            }
        }
Example #2
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);
            }
        }