public async Task <AuthorizationData> SetAuthorizationDataAsync(Authentication authentication) { _authorizationData = new AuthorizationData { Authentication = authentication, DeveloperToken = (DeveloperToken != null) ? DeveloperToken : null }; ApiEnvironment environment = ((OAuthWebAuthCodeGrant)_authorizationData.Authentication).Environment; CustomerManagementExampleHelper CustomerManagementExampleHelper = new CustomerManagementExampleHelper(null); CustomerManagementExampleHelper.CustomerManagementService = new ServiceClient <ICustomerManagementService>(_authorizationData, environment); var getUserResponse = await CustomerManagementExampleHelper.GetUserAsync(null, true); var user = getUserResponse.User; var predicate = new Predicate { Field = "UserId", Operator = PredicateOperator.Equals, Value = user.Id.ToString() }; var paging = new Paging { Index = 0, Size = 10 }; var accounts = (await CustomerManagementExampleHelper.SearchAccountsAsync( new[] { predicate }, null, paging)).Accounts.ToArray(); //var accounts = await SearchAccountsByUserIdAsync(user.Id); if (accounts.Length <= 0) { return(null); } _authorizationData.AccountId = (long)accounts[0].Id; _authorizationData.CustomerId = (int)accounts[0].ParentCustomerId; return(_authorizationData); }
public async override Task RunAsync(AuthorizationData authorizationData) { try { ApiEnvironment environment = ((OAuthDesktopMobileAuthCodeGrant)authorizationData.Authentication).Environment; CustomerManagementExampleHelper CustomerManagementExampleHelper = new CustomerManagementExampleHelper( OutputStatusMessageDefault: this.OutputStatusMessage); CustomerManagementExampleHelper.CustomerManagementService = new ServiceClient <ICustomerManagementService>( authorizationData: authorizationData, environment: environment); OutputStatusMessage("-----\nGetUser:"******"User:"******"CustomerRoles:"); CustomerManagementExampleHelper.OutputArrayOfCustomerRole(getUserResponse.CustomerRoles); // Search for the accounts that the user can access. // To retrieve more than 100 accounts, increase the page size up to 1,000. // To retrieve more than 1,000 accounts you'll need to add paging. var predicate = new Predicate { Field = "UserId", Operator = PredicateOperator.Equals, Value = user.Id.ToString() }; var paging = new Paging { Index = 0, Size = 100 }; OutputStatusMessage("-----\nSearchAccounts:"); var accounts = (await CustomerManagementExampleHelper.SearchAccountsAsync( predicates: new[] { predicate }, ordering: null, pageInfo: paging))?.Accounts.ToArray(); OutputStatusMessage("Accounts:"); CustomerManagementExampleHelper.OutputArrayOfAdvertiserAccount(accounts); HashSet <long> distinctCustomerIds = new HashSet <long>(); foreach (var account in accounts) { distinctCustomerIds.Add(account.ParentCustomerId); } foreach (var customerId in distinctCustomerIds) { // You can find out which pilot features the customer is able to use. // Each account could belong to a different customer, so use the customer ID in each account. OutputStatusMessage("-----\nGetCustomerPilotFeatures:"); OutputStatusMessage(string.Format("Requested by CustomerId: {0}", customerId)); var featurePilotFlags = (await CustomerManagementExampleHelper.GetCustomerPilotFeaturesAsync( customerId: customerId)).FeaturePilotFlags; OutputStatusMessage("Customer Pilot flags:"); OutputStatusMessage(string.Join("; ", featurePilotFlags.Select(flag => string.Format("{0}", flag)))); } } // Catch authentication exceptions catch (OAuthTokenRequestException ex) { OutputStatusMessage(string.Format("Couldn't get OAuth tokens. Error: {0}. Description: {1}", ex.Details.Error, ex.Details.Description)); } // Catch Customer Management service exceptions catch (FaultException <Microsoft.BingAds.V13.CustomerManagement.AdApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.Errors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (FaultException <Microsoft.BingAds.V13.CustomerManagement.ApiFault> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.OperationErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (Exception ex) { OutputStatusMessage(ex.Message); } }
public async override Task RunAsync(AuthorizationData authorizationData) { try { CustomerManagementExampleHelper CustomerManagementExampleHelper = new CustomerManagementExampleHelper(this.OutputStatusMessage); CustomerManagementExampleHelper.CustomerManagementService = new ServiceClient <ICustomerManagementService>(authorizationData); var getUserResponse = await CustomerManagementExampleHelper.GetUserAsync(null); var user = getUserResponse.User; // Search for the Bing Ads accounts that the user can access. var predicate = new Predicate { Field = "UserId", Operator = PredicateOperator.Equals, Value = user.Id.ToString() }; var paging = new Paging { Index = 0, Size = 10 }; var request = new SearchAccountsRequest { Ordering = null, PageInfo = paging, Predicates = new[] { predicate } }; var accounts = (await CustomerManagementExampleHelper.SearchAccountsAsync( new[] { predicate }, null, paging ))?.Accounts.ToArray(); OutputStatusMessage("The user can access the following Bing Ads accounts: \n"); foreach (var account in accounts) { CustomerManagementExampleHelper.OutputAccount(account); // You can find out which pilot features the customer is able to use. // Each account could belong to a different customer, so use the customer ID in each account. var featurePilotFlags = (await CustomerManagementExampleHelper.GetCustomerPilotFeaturesAsync(account.ParentCustomerId)).FeaturePilotFlags; OutputStatusMessage("Customer Pilot flags:"); OutputStatusMessage(string.Join("; ", featurePilotFlags.Select(flag => string.Format("{0}", flag)))); } } // Catch authentication exceptions catch (OAuthTokenRequestException ex) { OutputStatusMessage(string.Format("Couldn't get OAuth tokens. Error: {0}. Description: {1}", ex.Details.Error, ex.Details.Description)); } // Catch Customer Management service exceptions catch (FaultException <Microsoft.BingAds.V11.CustomerManagement.AdApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.Errors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (FaultException <Microsoft.BingAds.V11.CustomerManagement.ApiFault> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.OperationErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (Exception ex) { OutputStatusMessage(ex.Message); } }