Exemplo n.º 1
0
        /// <summary>
        /// Get Access token from AWeber (final step in OAuth) so we have a permanent token to use when performing API calls
        /// </summary>
        public String get_access_token()
        {
            // Build Post Data
            OAuth.Request request = new OAuth.Request();

            // Set Values
            request.oauth_verifier        = adapter.OAuthVerifier;
            request.oauth_consumer_key    = adapter.ConsumerKey;
            request.oauth_consumer_secret = adapter.ConsumerSecret;
            request.oauth_token           = adapter.OAuthToken;
            request.oauth_token_secret    = adapter.OAuthTokenSecret;

            // Build custom parameters for this OAuth Request
            SortedList <String, String> parameters = new SortedList <string, string>();

            parameters.Add("oauth_verifier", request.oauth_verifier);

            // Build request
            request.Build(parameters, Settings.accessToken);

            WebClient client = new WebClient();

            client.Headers["Content-type"] = "application/x-www-form-urlencoded";

            String response = String.Empty;

            try
            {
                // Get response
                response = client.UploadString(OAuth.Settings.accessToken, request.Parameters);
            }
            catch (WebException ex)
            {
                // Throw Error Codes back to client
                // Client responsibily to handle them
                throw ex;
            }

            // Save Access Token (Replace existing access tokens as they have now expired)
            SortedList <String, String> responseValues = new SortedList <string, string>();

            String[] keyValuePair = response.Split('&');

            foreach (String pair in keyValuePair)
            {
                String[] split = pair.Split('=');
                responseValues.Add(split[0], split[1]);
            }

            adapter.OAuthToken       = responseValues["oauth_token"];
            adapter.OAuthTokenSecret = responseValues["oauth_token_secret"];

            return(adapter.OAuthToken);
        }
Exemplo n.º 2
0
        public OAuth.Request BuildRequest()
        {
            // Build Post Data
            OAuth.Request request = new OAuth.Request();

            // Set Values
            request.oauth_consumer_key    = ConsumerKey;
            request.oauth_consumer_secret = ConsumerSecret;
            request.oauth_token           = OAuthToken;
            request.oauth_token_secret    = OAuthTokenSecret;
            request.oauth_verifier        = OAuthVerifier;

            return(request);
        }
Exemplo n.º 3
0
        ///
        /// Calls during authentication require a POST request
        ///

        #region Authentication

        /// <summary>
        /// Will issue the appropriate data to start OAuth 1.0 authorization
        /// </summary>
        /// <param name="callbackUrl">The callback url</param>
        public void get_request_token(String callbackUrl)
        {
            // Build Post Data
            OAuth.Request request = new OAuth.Request();

            // Set Values
            request.oauth_callback        = callbackUrl;
            request.oauth_consumer_key    = adapter.ConsumerKey;
            request.oauth_consumer_secret = adapter.ConsumerSecret;

            // Build custom parameters for this OAuth Request
            SortedList <String, String> parameters = new SortedList <string, string>();

            parameters.Add("oauth_callback", callbackUrl);

            // Build remainder of request
            request.Build(parameters, Settings.requestToken);

            // Send request to Aweber
            WebClient client = new WebClient();

            client.Headers["Content-type"] = "application/x-www-form-urlencoded";

            String response = String.Empty;

            try
            {
                // Get response
                response = client.UploadString(OAuth.Settings.requestToken, request.Parameters);
            }
            catch (WebException ex)
            {
                // Example on how to get the response text from the exception if needed

                //HttpWebResponse httpResponse = (HttpWebResponse)ex.Response;

                //using (Stream data = ex.Response.GetResponseStream())
                //{
                //    String text = new StreamReader(data).ReadToEnd();

                //}

                // Throw Error Codes back to client
                // Client responsibily to handle them
                throw ex;
            }

            // Parse Request Token
            SortedList <String, String> responseValues = new SortedList <string, string>();

            String[] keyValuePair = response.Split('&');

            foreach (String pair in keyValuePair)
            {
                String[] split = pair.Split('=');
                responseValues.Add(split[0], split[1]);
            }

            adapter.OAuthToken       = responseValues["oauth_token"];
            adapter.OAuthTokenSecret = responseValues["oauth_token_secret"];
        }
Exemplo n.º 4
0
        /// <summary>
        /// Connects to the API, issues the command and retrieves the JSON response
        /// </summary>
        /// <param name="url">The base url to contact for the request</param>
        /// <returns>The JSON returned</returns>
        public String GetResponse(String url)
        {
            // Build Post Data
            OAuth.Request request = new OAuth.Request();

            // Set Values
            request.oauth_consumer_key    = ConsumerKey;
            request.oauth_consumer_secret = ConsumerSecret;
            request.oauth_token           = OAuthToken;
            request.oauth_token_secret    = OAuthTokenSecret;
            request.oauth_verifier        = OAuthVerifier;

            // Build custom parameters for this OAuth Request
            SortedList <String, String> parameters = new SortedList <string, string>();

            String par = String.Empty;

            if (url.Contains("?"))
            {
                par = url.Substring(url.LastIndexOf("?") + 1);

                String[] values = par.Split('&');

                foreach (String value in values)
                {
                    String[] keyValue = value.Split('=');

                    parameters.Add(keyValue[0], keyValue[1]);
                }

                url = url.Substring(0, url.LastIndexOf("?"));

                if (url.Contains("http://"))
                {
                    url = url.Replace("http://", "https://");
                }
            }

            // Build request
            request.Build(parameters, url, "GET");

            WebClient client = new WebClient();

            client.Headers["Content-type"] = "application/x-www-form-urlencoded";

            String response = String.Empty;

            try
            {
                // Get response
                response = client.DownloadString(url + "?" + request.Parameters);
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
                {
                    var resp = (HttpWebResponse)ex.Response;
                    if (resp.StatusCode == HttpStatusCode.NotFound)
                    {
                        // 404 error
                        return(String.Empty);
                    }
                    else
                    {
                        throw ex;
                    }
                }
                else
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                // Throw Error Codes back to client
                // Client responsibility to handle them
                throw ex;
            }

            return(response);
        }