Beispiel #1
0
        /// <summary>
        /// Generates a ClientLogin token for use with various Ads APIs.
        /// </summary>
        /// <returns>The token string.</returns>
        /// <exception cref="AuthTokenException">If the token cannot be obtained,
        /// then an AuthTokenException is thrown with appropriate error code.
        /// </exception>
        private string GenerateToken()
        {
            WebRequest webRequest = HttpWebRequest.Create(url);

            webRequest.Method      = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";

            webRequest.Proxy   = config.Proxy;
            webRequest.Timeout = config.Timeout;

            string postParams =
                "accountType=" + HttpUtility.UrlEncode(ACCOUNT_TYPE) +
                "&Email=" + HttpUtility.UrlEncode(Email) +
                "&Passwd=" + HttpUtility.UrlEncode(Password) +
                "&service=" + HttpUtility.UrlEncode(service) +
                "&source=" + HttpUtility.UrlEncode(SOURCE);

            byte[] postBytes = Encoding.UTF8.GetBytes(postParams);
            webRequest.ContentLength = postBytes.Length;

            using (Stream strmReq = webRequest.GetRequestStream()) {
                strmReq.Write(postBytes, 0, postBytes.Length);
            }

            Dictionary <string, string> tblResponse = null;
            WebResponse response = null;

            try {
                response    = webRequest.GetResponse();
                tblResponse = ParseResponse(response);
            } catch (WebException ex) {
                AuthTokenException authException = ExtractException(ex);
                throw authException;
            } finally {
                if (response != null)
                {
                    response.Close();
                }
            }

            if (tblResponse.ContainsKey("Auth"))
            {
                return((string)tblResponse["Auth"]);
            }
            else
            {
                throw new AuthTokenException(AuthTokenErrorCode.Unknown, null, String.Empty, null,
                                             "Login failed - Could not find Auth key in response", null);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Generates a ClientLogin token for use with various Ads APIs.
        /// </summary>
        /// <returns>The token string.</returns>
        /// <exception cref="AuthTokenException">If the token cannot be obtained,
        /// then an AuthTokenException is thrown with appropriate error code.
        /// </exception>
        private string GenerateToken()
        {
            WebRequest webRequest = HttpWebRequest.Create(url);

            webRequest.Method      = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";

            webRequest.Proxy   = config.Proxy;
            webRequest.Timeout = config.Timeout;

            string postParams =
                "accountType=" + HttpUtility.UrlEncode(ACCOUNT_TYPE) +
                "&Email=" + HttpUtility.UrlEncode(Email) +
                "&Passwd=" + HttpUtility.UrlEncode(Password) +
                "&service=" + HttpUtility.UrlEncode(service) +
                "&source=" + HttpUtility.UrlEncode(SOURCE);

            byte[] postBytes = Encoding.UTF8.GetBytes(postParams);
            webRequest.ContentLength = postBytes.Length;

            using (Stream strmReq = webRequest.GetRequestStream()) {
                strmReq.Write(postBytes, 0, postBytes.Length);
            }

            LogEntry logEntry = new LogEntry(config, new DefaultDateTimeProvider());

            logEntry.LogRequest(webRequest, postParams, REQUEST_HEADERS_TO_MASK);

            Dictionary <string, string> tblResponse = null;
            WebResponse response = null;

            try {
                response = webRequest.GetResponse();
                string contents = MediaUtilities.GetStreamContentsAsString(response.GetResponseStream());
                logEntry.LogResponse(response, false, contents, RESPONSE_FIELDS_TO_MASK,
                                     new KeyValueMessageFormatter());
                logEntry.Flush();

                tblResponse = ParseResponse(contents);
            } catch (WebException e) {
                string contents = "";
                response = e.Response;

                try {
                    contents = MediaUtilities.GetStreamContentsAsString(response.GetResponseStream());
                } catch {
                    contents = e.Message;
                }

                logEntry.LogResponse(response, true, contents, RESPONSE_FIELDS_TO_MASK,
                                     new KeyValueMessageFormatter());
                logEntry.Flush();

                AuthTokenException authException = ExtractException(e, contents);
                throw authException;
            } finally {
                if (response != null)
                {
                    response.Close();
                }
            }

            if (tblResponse.ContainsKey("Auth"))
            {
                return((string)tblResponse["Auth"]);
            }
            else
            {
                throw new AuthTokenException(AuthTokenErrorCode.Unknown, null, String.Empty, null,
                                             "Login failed - Could not find Auth key in response", null);
            }
        }