Example #1
0
        /// <summary>
        /// Sets the request header namespace in outgoing Soap Requests.
        /// </summary>
        /// <param name="signature">The service creation parameters.</param>
        /// <param name="service">The service object for which RequestHeader
        /// needs to be patched.</param>
        private static void SetRequestHeaderNameSpace(DfaServiceSignature signature,
                                                      AdsClient service)
        {
            // Set the header namespace prefix. For all /cm services, the members
            // shouldn't have xmlns. For all other services, the members should have
            // /cm as xmlns.
            object[] attributes = service.GetType().GetCustomAttributes(false);
            foreach (object attribute in attributes)
            {
                if (attribute is WebServiceBindingAttribute)
                {
                    WebServiceBindingAttribute binding = (WebServiceBindingAttribute)attribute;

                    string        xmlns            = binding.Namespace;
                    RequestHeader svcRequestHeader = null;
                    PropertyInfo  propInfo         = service.GetType().GetProperty("RequestHeader");
                    if (propInfo != null)
                    {
                        svcRequestHeader = (RequestHeader)propInfo.GetValue(service, null);

                        if (svcRequestHeader != null)
                        {
                            PropertyInfo wsPropInfo = svcRequestHeader.GetType().GetProperty("TargetNamespace");
                            if (wsPropInfo != null)
                            {
                                wsPropInfo.SetValue(svcRequestHeader, xmlns, null);
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Generates a login token for authenticating DFA API calls.
        /// </summary>
        /// <param name="user">The user for which token is generated.</param>
        /// <param name="serviceVersion">The service version.</param>
        /// <returns>A token which may be used for future API calls.</returns>
        private static UserToken GenerateAuthenticationToken(AdsUser user, string serviceVersion)
        {
            DfaAppConfig config = (DfaAppConfig)user.Config;

            if (!String.IsNullOrEmpty(config.DfaAuthToken))
            {
                return(new UserToken(config.DfaUserName, config.DfaAuthToken));
            }
            if (config.AuthorizationMethod == DfaAuthorizationMethod.LoginService)
            {
                if (string.IsNullOrEmpty(config.DfaUserName))
                {
                    throw new ArgumentNullException(DfaErrorMessages.UserNameCannotBeEmpty);
                }
                if (string.IsNullOrEmpty(config.DfaPassword))
                {
                    throw new ArgumentNullException(DfaErrorMessages.PasswordCannotBeEmpty);
                }
            }
            try {
                DfaServiceSignature loginServiceSignature = new DfaServiceSignature(serviceVersion,
                                                                                    "LoginRemoteService");
                AdsClient loginService = user.GetService(loginServiceSignature, config.DfaApiServer);
                object    userProfile  = loginService.GetType().GetMethod("authenticate").Invoke(
                    loginService, new object[] { config.DfaUserName, config.DfaPassword });
                return(new UserToken(
                           userProfile.GetType().GetProperty("name").GetValue(userProfile, null).ToString(),
                           userProfile.GetType().GetProperty("token").GetValue(userProfile, null).ToString()));
            } catch (Exception ex) {
                throw new DfaException("Failed to authenticate user. See inner exception for details.", ex);
            }
        }
        /// <summary>
        /// Fix the request header namespace in outgoing Soap Requests, so that
        /// cross namespace requests can work properly.
        /// </summary>
        /// <param name="signature">The service creation parameters.</param>
        /// <param name="service">The service object for which RequestHeader
        /// needs to be patched.</param>
        private static void FixRequestHeaderNameSpace(AdWordsServiceSignature signature,
                                                      AdsClient service)
        {
            // Set the header namespace prefix. For all /cm services, the members
            // shouldn't have xmlns. For all other services, the members should have
            // /cm as xmlns.
            object[] attributes = service.GetType().GetCustomAttributes(false);
            foreach (object attribute in attributes)
            {
                if (attribute is WebServiceBindingAttribute)
                {
                    WebServiceBindingAttribute binding = (WebServiceBindingAttribute)attribute;
                    string delimiter = "/api/adwords/";
                    string xmlns     = String.Join("", new String[] {
                        binding.Namespace.Substring(0, binding.Namespace.IndexOf(delimiter) +
                                                    delimiter.Length), "cm/", signature.Version
                    });
                    if (xmlns == binding.Namespace)
                    {
                        xmlns = "";
                    }

                    RequestHeader svcRequestHeader = null;
                    PropertyInfo  propInfo         = service.GetType().GetProperty("RequestHeader");
                    if (propInfo != null)
                    {
                        svcRequestHeader = (RequestHeader)propInfo.GetValue(service, null);

                        if (svcRequestHeader != null)
                        {
                            PropertyInfo wsPropInfo = svcRequestHeader.GetType().GetProperty("TargetNamespace");
                            if (wsPropInfo != null)
                            {
                                wsPropInfo.SetValue(svcRequestHeader, xmlns, null);
                            }
                        }
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        /// Create a service object.
        /// </summary>
        /// <param name="signature">Signature of the service being created.</param>
        /// <param name="user">The user for which the service is being created.
        /// <param name="serverUrl">The server to which the API calls should be
        /// made.</param>
        /// </param>
        /// <returns>An object of the desired service type.</returns>
        public override AdsClient CreateService(ServiceSignature signature, AdsUser user,
                                                Uri serverUrl)
        {
            DfaAppConfig config = (DfaAppConfig)base.Config;

            if (serverUrl == null)
            {
                serverUrl = new Uri(config.DfaApiServer);
            }

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

            CheckServicePreconditions(signature);

            DfaServiceSignature dfaapiSignature = signature as DfaServiceSignature;

            AdsClient service = (AdsClient)Activator.CreateInstance(dfaapiSignature.ServiceType);

            if (config.Proxy != null)
            {
                service.Proxy = config.Proxy;
            }

            service.Timeout = config.Timeout;
            service.Url     = string.Format("{0}{1}/api/dfa-api/{2}",
                                            serverUrl, dfaapiSignature.Version, dfaapiSignature.ServiceEndpoint);
            service.UserAgent = config.GetUserAgent();

            service.User      = user;
            service.Signature = signature;

            service.GetType().GetProperty("RequestHeader").SetValue(service,
                                                                    GetRequestHeader(), null);
            SetRequestHeaderNameSpace(signature as DfaServiceSignature, service);

            return(service);
        }
        /// <summary>
        /// Create a service object.
        /// </summary>
        /// <param name="signature">Signature of the service being created.</param>
        /// <param name="user">The user for which the service is being created.
        /// <param name="serverUrl">The server to which the API calls should be
        /// made.</param>
        /// </param>
        /// <returns>An object of the desired service type.</returns>
        public override AdsClient CreateService(ServiceSignature signature, AdsUser user,
                                                Uri serverUrl)
        {
            DfpAppConfig dfpConfig = (DfpAppConfig)Config;

            if (serverUrl == null)
            {
                serverUrl = new Uri(dfpConfig.DfpApiServer);
            }

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

            CheckServicePreconditions(signature);

            DfpServiceSignature dfpapiSignature = signature as DfpServiceSignature;
            EndpointAddress     endpoint        = new EndpointAddress(string.Format(ENDPOINT_TEMPLATE,
                                                                                    serverUrl, dfpapiSignature.Version, dfpapiSignature.ServiceName));

            // Create the binding for the service
            BasicHttpBinding binding = new BasicHttpBinding();

            binding.Security.Mode = BasicHttpSecurityMode.Transport;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
            binding.MaxReceivedMessageSize = int.MaxValue;
            binding.TextEncoding           = Encoding.UTF8;

            AdsClient service = (AdsClient)Activator.CreateInstance(
                dfpapiSignature.ServiceType,
                new object[] { binding, endpoint });

            ServiceEndpoint serviceEndpoint =
                (ServiceEndpoint)service.GetType().GetProperty("Endpoint").GetValue(service, null);

            AdsServiceInspectorBehavior inspectorBehavior = new AdsServiceInspectorBehavior();

            inspectorBehavior.Add(new OAuth2ClientMessageInspector(user.OAuthProvider));

            RequestHeader clonedHeader = (RequestHeader)requestHeader.Clone();

            clonedHeader.Version = dfpapiSignature.Version;
            inspectorBehavior.Add(new DfpSoapHeaderInspector()
            {
                RequestHeader = clonedHeader,
                Config        = dfpConfig
            });
            inspectorBehavior.Add(new SoapListenerInspector(user, dfpapiSignature.ServiceName));
            inspectorBehavior.Add(new SoapFaultInspector <DfpApiException>()
            {
                ErrorType = dfpapiSignature.ServiceType.Assembly.GetType(
                    dfpapiSignature.ServiceType.Namespace + ".ApiException"),
            });
#if NET452
            serviceEndpoint.Behaviors.Add(inspectorBehavior);
#else
            serviceEndpoint.EndpointBehaviors.Add(inspectorBehavior);
#endif

            if (dfpConfig.Proxy != null)
            {
                service.Proxy = dfpConfig.Proxy;
            }
            service.EnableDecompression = dfpConfig.EnableGzipCompression;
            service.Timeout             = dfpConfig.Timeout;
            service.UserAgent           = dfpConfig.GetUserAgent();

            service.Signature = signature;
            service.User      = user;
            return(service);
        }
    /// <summary>
    /// Fix the request header namespace in outgoing Soap Requests, so that
    /// cross namespace requests can work properly.
    /// </summary>
    /// <param name="signature">The service creation parameters.</param>
    /// <param name="service">The service object for which RequestHeader
    /// needs to be patched.</param>
    private static void FixRequestHeaderNameSpace(AdWordsServiceSignature signature,
        AdsClient service) {
      // Set the header namespace prefix. For all /cm services, the members
      // shouldn't have xmlns. For all other services, the members should have
      // /cm as xmlns.
      object[] attributes = service.GetType().GetCustomAttributes(false);
      foreach (object attribute in attributes) {
        if (attribute is WebServiceBindingAttribute) {
          WebServiceBindingAttribute binding = (WebServiceBindingAttribute) attribute;
          string delimiter = "/api/adwords/";
          string xmlns = String.Join("", new String[] {
              binding.Namespace.Substring(0, binding.Namespace.IndexOf(delimiter) +
                  delimiter.Length), "cm/", signature.Version});
          if (xmlns == binding.Namespace) {
            xmlns = "";
          }

          RequestHeader svcRequestHeader = null;
          PropertyInfo propInfo = service.GetType().GetProperty("RequestHeader");
          if (propInfo != null) {
            svcRequestHeader = (RequestHeader) propInfo.GetValue(service, null);

            if (svcRequestHeader != null) {
              PropertyInfo wsPropInfo = svcRequestHeader.GetType().GetProperty("TargetNamespace");
              if (wsPropInfo != null) {
                wsPropInfo.SetValue(svcRequestHeader, xmlns, null);
              }
            }
          }
        }
      }
    }
        /// <summary>
        /// Sets the request header namespace in outgoing Soap Requests.
        /// </summary>
        /// <param name="signature">The service creation parameters.</param>
        /// <param name="service">The service object for which RequestHeader
        /// needs to be patched.</param>
        private static void SetRequestHeaderNameSpace(DfaServiceSignature signature,
        AdsClient service)
        {
            // Set the header namespace prefix. For all /cm services, the members
              // shouldn't have xmlns. For all other services, the members should have
              // /cm as xmlns.
              object[] attributes = service.GetType().GetCustomAttributes(false);
              foreach (object attribute in attributes) {
            if (attribute is WebServiceBindingAttribute) {
              WebServiceBindingAttribute binding = (WebServiceBindingAttribute) attribute;

              string xmlns = binding.Namespace;
              RequestHeader svcRequestHeader = null;
              PropertyInfo propInfo = service.GetType().GetProperty("RequestHeader");
              if (propInfo != null) {
            svcRequestHeader = (RequestHeader) propInfo.GetValue(service, null);

            if (svcRequestHeader != null) {
              PropertyInfo wsPropInfo = svcRequestHeader.GetType().GetProperty("TargetNamespace");
              if (wsPropInfo != null) {
                wsPropInfo.SetValue(svcRequestHeader, xmlns, null);
              }
            }
              }
            }
              }
        }
Example #8
0
        /// <summary>
        /// Create a service object.
        /// </summary>
        /// <param name="signature">Signature of the service being created.</param>
        /// <param name="user">The user for which the service is being created.</param>
        /// <param name="serverUrl">The server to which the API calls should be
        /// made.</param>
        /// <returns>An object of the desired service type.</returns>
        public override AdsClient CreateService(ServiceSignature signature, AdsUser user,
                                                Uri serverUrl)
        {
            AdWordsAppConfig awConfig = (AdWordsAppConfig)Config;

            if (serverUrl == null)
            {
                serverUrl = new Uri(awConfig.AdWordsApiServer);
            }

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

            CheckServicePreconditions(signature);

            AdWordsServiceSignature awapiSignature = signature as AdWordsServiceSignature;
            EndpointAddress         endpoint       = new EndpointAddress(string.Format(ENDPOINT_TEMPLATE,
                                                                                       serverUrl, awapiSignature.GroupName, awapiSignature.Version,
                                                                                       awapiSignature.ServiceName));

            // Create the binding for the service.
            BasicHttpBinding binding = new BasicHttpBinding();

            // If the server end point is HTTP, then don't use security. This is used for testing
            // purposes only.
            if (endpoint.Uri.Scheme == "http")
            {
                binding.Security.Mode = BasicHttpSecurityMode.None;
            }
            else
            {
                binding.Security.Mode = BasicHttpSecurityMode.Transport;
            }

            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
            binding.MaxReceivedMessageSize = int.MaxValue;
            binding.TextEncoding           = Encoding.UTF8;

            AdsClient service = (AdsClient)Activator.CreateInstance(awapiSignature.ServiceType,
                                                                    new object[]
            {
                binding,
                endpoint
            });

            ServiceEndpoint serviceEndpoint =
                (ServiceEndpoint)service.GetType().GetProperty("Endpoint").GetValue(service, null);

            AdsServiceInspectorBehavior inspectorBehavior = new AdsServiceInspectorBehavior();

            // Add OAuth client message inspector only if the authorization method is OAuth2.
            // In testing mode, the authorization method is set to Insecure.
            if (awConfig.AuthorizationMethod == AdWordsAuthorizationMethod.OAuth2)
            {
                inspectorBehavior.Add(new OAuthClientMessageInspector(user.OAuthProvider));
            }

            RequestHeader clonedHeader = (RequestHeader)requestHeader.Clone();

            clonedHeader.Version   = awapiSignature.Version;
            clonedHeader.GroupName = awapiSignature.GroupName;
            inspectorBehavior.Add(new AdWordsSoapHeaderInspector()
            {
                RequestHeader = clonedHeader,
                User          = (AdWordsUser)user,
            });
            inspectorBehavior.Add(new SoapListenerInspector(user, awapiSignature.ServiceName));
            inspectorBehavior.Add(new SoapFaultInspector <AdWordsApiException>()
            {
                ErrorType =
                    awapiSignature.ServiceType.Assembly.GetType(
                        awapiSignature.ServiceType.Namespace + ".ApiException")
            });
#if NET452
            serviceEndpoint.Behaviors.Add(inspectorBehavior);
#else
            serviceEndpoint.EndpointBehaviors.Add(inspectorBehavior);
#endif

            if (awConfig.Proxy != null)
            {
                service.Proxy = awConfig.Proxy;
            }

            service.Timeout             = awConfig.Timeout;
            service.EnableDecompression = awConfig.EnableGzipCompression;
            service.User      = user;
            service.Signature = awapiSignature;
            return(service);
        }