/// <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); } String dfaUserName = config.DfaUserName; String dfaPassword = config.DfaPassword; if (config.AuthorizationMethod == DfaAuthorizationMethod.LoginService) { if (string.IsNullOrEmpty(dfaUserName)) { throw new ArgumentNullException(DfaErrorMessages.UserNameCannotBeEmpty); } if (string.IsNullOrEmpty(dfaPassword)) { throw new ArgumentNullException(DfaErrorMessages.PasswordCannotBeEmpty); } } else if (config.AuthorizationMethod == DfaAuthorizationMethod.OAuth2) { // DFA password should not be set when using OAuth2 dfaPassword = ""; } try { DfaServiceSignature loginServiceSignature = new DfaServiceSignature(serviceVersion, "LoginRemoteService"); AdsClient loginService = user.GetService(loginServiceSignature, config.DfaApiServer); object userProfile = loginService.GetType().GetMethod("authenticate").Invoke( loginService, new object[] {dfaUserName, 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> /// Gets the signature. /// </summary> /// <param name="user">The user for which service should be mocked.</param> /// <param name="signature">The service signature to be mocked.</param> /// <param name="mockType">Type of the mock service.</param> /// <returns>The mock service signature.</returns> public static ServiceSignature RegisterMockService(AdsUser user, ServiceSignature signature, Type mockType) { MockAdWordsServiceSignature mockSignature = MockAdWordsServiceSignature.FromSignature( signature, mockType); user.RegisterService(signature.Id, user.GetServiceFactory(mockSignature.Id)); return mockSignature; }
/// <summary> /// Initializes a new instance of the <see cref="BatchJobUtilities"/> /// class. /// </summary> /// <param name="user">AdWords user to be used along with this /// utilities object.</param> /// <param name="chunkSize">The chunk size to use for resumable upload.</param> /// <exception cref="ArgumentException">Thrown if <paramref name="chunkSie"/> /// is not a multiple of 256KB.</exception> public BatchJobUtilities(AdsUser user, int chunkSize) { if ((chunkSize % CHUNK_SIZE_ALIGN) != 0) { throw new ArgumentException("Chunk size should be a multiple of 256KB."); } this.user = user; this.CHUNK_SIZE = chunkSize; }
/// <summary> /// Create a service of desired type. /// </summary> /// <param name="signature">Signature of the service being created.</param> /// <param name="user">The user for which this service is being created. /// </param> /// <param name="serverUrl">The server to which the API calls should be /// made.</param> /// <returns>The service object.</returns> public override AdsClient CreateService(ServiceSignature signature, AdsUser user, Uri serverUrl) { AdWordsAppConfig awConfig = (AdWordsAppConfig) AppConfig; if (serverUrl == null) { serverUrl = new Uri(awConfig.LegacyAdWordsApiServer); } if (user == null) { throw new ArgumentNullException("user"); } if (signature == null) { throw new ArgumentNullException("signature"); } if (!(signature is LegacyAdwordsServiceSignature)) { throw new ArgumentException("Expecting a LegacyAdwordsApiServiceSignature object."); } LegacyAdwordsServiceSignature awapiSignature = (LegacyAdwordsServiceSignature) signature; AdsClient service = (AdsClient) Activator.CreateInstance(awapiSignature.ServiceType); Type type = service.GetType(); PropertyInfo propInfo = null; if (this.headers != null) { foreach (string key in headers.Keys) { propInfo = type.GetProperty(key); if (propInfo != null) { propInfo.SetValue(service, headers[key], null); } } } if (awConfig.Proxy != null) { service.Proxy = awConfig.Proxy; } service.Timeout = awConfig.Timeout; service.Url = String.Join("", new string[] {serverUrl.AbsoluteUri, "api/adwords/", awapiSignature.Version, "/", awapiSignature.ServiceName}); service.User = user; 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) { AdWordsAppConfig awConfig = (AdWordsAppConfig) Config; if (serverUrl == null) { serverUrl = new Uri(awConfig.AdWordsApiServer); } if (user == null) { throw new ArgumentNullException("user"); } if (signature == null) { throw new ArgumentNullException("signature"); } if (!(signature is AdWordsServiceSignature)) { throw new InvalidCastException(string.Format(CultureInfo.InvariantCulture, AdWordsErrorMessages.SignatureIsOfWrongType, typeof(AdWordsServiceSignature))); } AdWordsServiceSignature awapiSignature = signature as AdWordsServiceSignature; AdsClient service = (AdsClient) Activator.CreateInstance(awapiSignature.ServiceType); PropertyInfo propInfo = awapiSignature.ServiceType.GetProperty("RequestHeader"); if (propInfo != null) { RequestHeader clonedHeader = (RequestHeader) requestHeader.Clone(); clonedHeader.Version = awapiSignature.Version; clonedHeader.GroupName = awapiSignature.GroupName; propInfo.SetValue(service, clonedHeader, null); } if (awConfig.Proxy != null) { service.Proxy = awConfig.Proxy; } service.Timeout = awConfig.Timeout; service.Url = string.Format("{0}api/adwords/{1}/{2}/{3}", serverUrl.AbsoluteUri, awapiSignature.GroupName, awapiSignature.Version, awapiSignature.ServiceName); service.EnableDecompression = awConfig.EnableGzipCompression; service.User = user; service.Signature = awapiSignature; return service; }
/// <summary> /// Gets 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> public static UserToken GetAuthenticationToken(AdsUser user, string serviceVersion) { DfaAppConfig config = (DfaAppConfig) user.Config; if (string.IsNullOrEmpty(config.DfaUserName)) { throw new ArgumentNullException(DfaErrorMessages.UserNameCannotBeEmpty); } UserToken userToken = tokenCache.GetToken(config.DfaUserName); if (userToken == null) { lock (typeof(LoginUtil)) { userToken = tokenCache.GetToken(config.DfaUserName); if (userToken == null) { userToken = GenerateAuthenticationToken(user, serviceVersion); tokenCache.AddToken(config.DfaUserName, userToken); } } } return userToken; }
/// <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; AdsClient service = (AdsClient) Activator.CreateInstance(dfpapiSignature.ServiceType); PropertyInfo propInfo = dfpapiSignature.ServiceType.GetProperty("RequestHeader"); if (propInfo != null) { RequestHeader clonedHeader = (RequestHeader) requestHeader.Clone(); clonedHeader.Version = dfpapiSignature.Version; propInfo.SetValue(service, clonedHeader, null); } if (dfpConfig.Proxy != null) { service.Proxy = dfpConfig.Proxy; } service.Timeout = dfpConfig.Timeout; service.Url = string.Format("{0}apis/ads/publisher/{1}/{2}", serverUrl, dfpapiSignature.Version, dfpapiSignature.ServiceName); service.UserAgent = dfpConfig.GetUserAgent(); service.Signature = signature; service.User = user; return service; }
/// <summary> /// Initializes a new instance of the <see cref="AdWordsErrorHandler"/> class. /// </summary> /// <param name="user">The user.</param> public BulkJobErrorHandler(AdsUser user) : base(user.Config) { this.user = user; }
/// <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 abstract AdsClient CreateService(ServiceSignature signature, AdsUser user, Uri serverUrl);
/// <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> /// <param name="serverUrl"></param> /// <returns> /// An object of the desired service type. /// </returns> public override AdsClient CreateService(ServiceSignature signature, AdsUser user, Uri serverUrl) { return null; }
/// <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 abstract AdsClient CreateService(ServiceSignature signature, AdsUser user, Uri serverUrl);
/// <summary> /// Initializes a new instance of the <see cref="BatchJobUtilities"/> /// class. /// </summary> /// <param name="user">AdWords user to be used along with this /// utilities object.</param> public BatchJobUtilities(AdsUser user) : this(user, DEFAULT_CHUNK_SIZE) { }
/// <summary> /// Initializes a new instance of the <see cref="BatchJobUtilities"/> /// class. /// </summary> /// <param name="user">AdWords user to be used along with this /// utilities object.</param> /// <param name="useChunking">if the operations should be broken into /// smaller chunks before uploading to the server.</param> /// <param name="chunkSize">The chunk size to use for resumable upload.</param> /// <exception cref="ArgumentException">Thrown if <paramref name="chunkSie"/> /// is not a multiple of 256KB.</exception> /// <remarks>Use chunking if your network is spotty for uploads, or if it /// has restrictions such as speed limits or timeouts. Chunking makes your /// upload reliable when the network is unreliable, but it is inefficient /// over a good connection, since an HTTPs request has to be made for every /// chunk being uploaded.</remarks> public BatchJobUtilities(AdsUser user, bool useChunking, int chunkSize) : base(user, useChunking, chunkSize) { }
/// <summary> /// Initializes a new instance of the <see cref="BatchJobUtilities"/> /// class. /// </summary> /// <param name="user">AdWords user to be used along with this /// utilities object.</param> public BatchJobUtilities(AdsUser user) : base(user) { }
/// <summary> /// Initializes a new instance of the <see cref="AdsReportUtilities"/> /// class. /// </summary> /// <param name="user">AdWords user to be used along with this /// utilities object.</param> public AdsReportUtilities(AdsUser user) { this.user = user; }