|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Microsoft\BingAds\Samples\V13; |
| 4 | + |
| 5 | +require_once __DIR__ . "/../vendor/autoload.php"; |
| 6 | + |
| 7 | +require_once __DIR__ . "/CustomerManagementExampleHelper.php"; |
| 8 | + |
| 9 | +// Specify the Microsoft\BingAds\Auth classes that will be used. |
| 10 | + |
| 11 | +use Microsoft\BingAds\Auth\OAuthDesktopMobileAuthCodeGrant; |
| 12 | +use Microsoft\BingAds\Auth\OAuthWebAuthCodeGrant; |
| 13 | +use Microsoft\BingAds\Auth\AuthorizationData; |
| 14 | +use Microsoft\BingAds\Auth\OAuthTokenRequestException; |
| 15 | +use Microsoft\BingAds\Auth\ApiEnvironment; |
| 16 | +use Microsoft\BingAds\Auth\ServiceClient; |
| 17 | +use Microsoft\BingAds\Auth\ServiceClientType; |
| 18 | + |
| 19 | +// Specify the Microsoft\BingAds\Samples\V13 classes that will be used. |
| 20 | +use Microsoft\BingAds\Samples\V13\CustomerManagementExampleHelper; |
| 21 | + |
| 22 | +// Specify the Microsoft\BingAds\V13\CampaignManagement classes that will be used. |
| 23 | +use Microsoft\BingAds\V13\CampaignManagement\AdGroupCriterionType; |
| 24 | +use Microsoft\BingAds\V13\CampaignManagement\CampaignCriterionType; |
| 25 | +use Microsoft\BingAds\V13\CampaignManagement\CampaignType; |
| 26 | + |
| 27 | +// Specify the Microsoft\BingAds\V13\CustomerManagement classes that will be used. |
| 28 | + |
| 29 | +use Microsoft\BingAds\V13\CustomerManagement\GetUserRequest; |
| 30 | +use Microsoft\BingAds\V13\CustomerManagement\SearchAccountsRequest; |
| 31 | +use Microsoft\BingAds\V13\CustomerManagement\Paging; |
| 32 | +use Microsoft\BingAds\V13\CustomerManagement\Predicate; |
| 33 | +use Microsoft\BingAds\V13\CustomerManagement\PredicateOperator; |
| 34 | + |
| 35 | +use Exception; |
| 36 | + |
| 37 | +/** |
| 38 | + * Defines global settings that you can use for testing your application. |
| 39 | + * Your production implementation may vary, and you should always store sensitive information securely. |
| 40 | + */ |
| 41 | +final class AuthHelper { |
| 42 | + |
| 43 | + const DeveloperToken = 'BBD37VB98'; // For sandbox use BBD37VB98 |
| 44 | + const ApiEnvironment = ApiEnvironment::Sandbox; |
| 45 | + const OAuthRefreshTokenPath = 'refresh.txt'; |
| 46 | + const ClientId = 'ClientIdGoesHere'; |
| 47 | + |
| 48 | + const CampaignTypes = |
| 49 | + CampaignType::Audience . ' ' . |
| 50 | + CampaignType::Search . ' ' . |
| 51 | + CampaignType::Shopping . ' ' . |
| 52 | + CampaignType::DynamicSearchAds; |
| 53 | + |
| 54 | + const AllTargetCampaignCriterionTypes = |
| 55 | + CampaignCriterionType::Age . ' ' . |
| 56 | + CampaignCriterionType::DayTime . ' ' . |
| 57 | + CampaignCriterionType::Device . ' ' . |
| 58 | + CampaignCriterionType::Gender . ' ' . |
| 59 | + CampaignCriterionType::Location . ' ' . |
| 60 | + CampaignCriterionType::LocationIntent . ' ' . |
| 61 | + CampaignCriterionType::Radius; |
| 62 | + |
| 63 | + const AllTargetAdGroupCriterionTypes = |
| 64 | + AdGroupCriterionType::Age . ' ' . |
| 65 | + AdGroupCriterionType::DayTime . ' ' . |
| 66 | + AdGroupCriterionType::Device . ' ' . |
| 67 | + AdGroupCriterionType::Gender . ' ' . |
| 68 | + AdGroupCriterionType::Location . ' ' . |
| 69 | + AdGroupCriterionType::LocationIntent . ' ' . |
| 70 | + AdGroupCriterionType::Radius; |
| 71 | + |
| 72 | + static function Authenticate() |
| 73 | + { |
| 74 | + // Disable WSDL caching. |
| 75 | + ini_set("soap.wsdl_cache_enabled", "0"); |
| 76 | + ini_set("soap.wsdl_cache_ttl", "0"); |
| 77 | + |
| 78 | + // Authenticate with a Microsoft Account. |
| 79 | + AuthHelper::AuthenticateWithOAuth(); |
| 80 | + |
| 81 | + $GLOBALS['CustomerManagementProxy'] = new ServiceClient( |
| 82 | + ServiceClientType::CustomerManagementVersion13, |
| 83 | + $GLOBALS['AuthorizationData'], |
| 84 | + AuthHelper::GetApiEnvironment() |
| 85 | + ); |
| 86 | + |
| 87 | + // Set to an empty user identifier to get the current authenticated user, |
| 88 | + // and then search for accounts the user can access. |
| 89 | + $user = CustomerManagementExampleHelper::GetUser( |
| 90 | + null, |
| 91 | + true |
| 92 | + )->User; |
| 93 | + |
| 94 | + // To retrieve more than 100 accounts, increase the page size up to 1,000. |
| 95 | + // To retrieve more than 1,000 accounts you'll need to implement paging. |
| 96 | + $accounts = AuthHelper::SearchAccountsByUserId( |
| 97 | + $user->Id, |
| 98 | + 0, |
| 99 | + 100 |
| 100 | + )->Accounts; |
| 101 | + |
| 102 | + // We'll use the first account by default for the examples. |
| 103 | + |
| 104 | + $GLOBALS['AuthorizationData']->AccountId = $accounts->AdvertiserAccount[0]->Id; |
| 105 | + $GLOBALS['AuthorizationData']->CustomerId = $accounts->AdvertiserAccount[0]->ParentCustomerId; |
| 106 | + |
| 107 | + $GLOBALS['AdInsightProxy'] = new ServiceClient( |
| 108 | + ServiceClientType::AdInsightVersion13, |
| 109 | + $GLOBALS['AuthorizationData'], |
| 110 | + AuthHelper::GetApiEnvironment() |
| 111 | + ); |
| 112 | + |
| 113 | + $GLOBALS['BulkProxy'] = new ServiceClient( |
| 114 | + ServiceClientType::BulkVersion13, |
| 115 | + $GLOBALS['AuthorizationData'], |
| 116 | + AuthHelper::GetApiEnvironment() |
| 117 | + ); |
| 118 | + |
| 119 | + $GLOBALS['CampaignManagementProxy'] = new ServiceClient( |
| 120 | + ServiceClientType::CampaignManagementVersion13, |
| 121 | + $GLOBALS['AuthorizationData'], |
| 122 | + AuthHelper::GetApiEnvironment() |
| 123 | + ); |
| 124 | + |
| 125 | + $GLOBALS['CustomerManagementProxy'] = new ServiceClient( |
| 126 | + ServiceClientType::CustomerManagementVersion13, |
| 127 | + $GLOBALS['AuthorizationData'], |
| 128 | + AuthHelper::GetApiEnvironment() |
| 129 | + ); |
| 130 | + |
| 131 | + $GLOBALS['ReportingProxy'] = new ServiceClient( |
| 132 | + ServiceClientType::ReportingVersion13, |
| 133 | + $GLOBALS['AuthorizationData'], |
| 134 | + AuthHelper::GetApiEnvironment() |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + static function SearchAccountsByUserId($userId, $pageIndex, $pageSize) |
| 139 | + { |
| 140 | + $GLOBALS['Proxy'] = $GLOBALS['CustomerManagementProxy']; |
| 141 | + |
| 142 | + // Specify the page index and number of account results per page. |
| 143 | + |
| 144 | + $pageInfo = new Paging(); |
| 145 | + $pageInfo->Index = $pageIndex; |
| 146 | + $pageInfo->Size = $pageSize; |
| 147 | + |
| 148 | + $predicate = new Predicate(); |
| 149 | + $predicate->Field = "UserId"; |
| 150 | + $predicate->Operator = PredicateOperator::Equals; |
| 151 | + $predicate->Value = $userId; |
| 152 | + |
| 153 | + $request = new SearchAccountsRequest(); |
| 154 | + $request->Ordering = null; |
| 155 | + $request->PageInfo = $pageInfo; |
| 156 | + $request->Predicates = array($predicate); |
| 157 | + |
| 158 | + return $GLOBALS['Proxy']->GetService()->SearchAccounts($request); |
| 159 | + } |
| 160 | + |
| 161 | + // Sets the global authorization data instance with OAuthDesktopMobileAuthCodeGrant. |
| 162 | + |
| 163 | + static function AuthenticateWithOAuth() |
| 164 | + { |
| 165 | + $authentication = (new OAuthDesktopMobileAuthCodeGrant()) |
| 166 | + ->withEnvironment(AuthHelper::ApiEnvironment) |
| 167 | + ->withClientId(AuthHelper::ClientId); |
| 168 | + |
| 169 | + $GLOBALS['AuthorizationData'] = (new AuthorizationData()) |
| 170 | + ->withAuthentication($authentication) |
| 171 | + ->withDeveloperToken(AuthHelper::DeveloperToken); |
| 172 | + |
| 173 | + try |
| 174 | + { |
| 175 | + $refreshToken = AuthHelper::ReadOAuthRefreshToken(); |
| 176 | + |
| 177 | + if($refreshToken != null) |
| 178 | + { |
| 179 | + $GLOBALS['AuthorizationData']->Authentication->RequestOAuthTokensByRefreshToken($refreshToken); |
| 180 | + AuthHelper::WriteOAuthRefreshToken( |
| 181 | + $GLOBALS['AuthorizationData']->Authentication->OAuthTokens->RefreshToken |
| 182 | + ); |
| 183 | + } |
| 184 | + else |
| 185 | + { |
| 186 | + AuthHelper::RequestUserConsent(); |
| 187 | + } |
| 188 | + } |
| 189 | + catch(OAuthTokenRequestException $e) |
| 190 | + { |
| 191 | + printf("Error: %s\n", $e->Error); |
| 192 | + printf("Description: %s\n", $e->Description); |
| 193 | + |
| 194 | + AuthHelper::RequestUserConsent(); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + static function RequestUserConsent() |
| 199 | + { |
| 200 | + print "You need to provide consent for the application to access your Bing Ads Bing Ads accounts. " . |
| 201 | + "Copy and paste this authorization endpoint into a web browser and sign in with a Microsoft account " . |
| 202 | + "with access to a Bing Ads account: \n\n" . $GLOBALS['AuthorizationData']->Authentication->GetAuthorizationEndpoint() . |
| 203 | + "\n\nAfter you have granted consent in the web browser for the application to access your Bing Ads accounts, " . |
| 204 | + "please enter the response URI that includes the authorization 'code' parameter: \n\n"; |
| 205 | + |
| 206 | + $responseUri = fgets(STDIN); |
| 207 | + print "\n"; |
| 208 | + |
| 209 | + $GLOBALS['AuthorizationData']->Authentication->RequestOAuthTokensByResponseUri(trim($responseUri)); |
| 210 | + AuthHelper::WriteOAuthRefreshToken($GLOBALS['AuthorizationData']->Authentication->OAuthTokens->RefreshToken); |
| 211 | + } |
| 212 | + |
| 213 | + static function GetApiEnvironment() |
| 214 | + { |
| 215 | + return AuthHelper::ApiEnvironment; |
| 216 | + } |
| 217 | + |
| 218 | + static function ReadOAuthRefreshToken() |
| 219 | + { |
| 220 | + $refreshToken = null; |
| 221 | + |
| 222 | + if (file_exists(AuthHelper::OAuthRefreshTokenPath) && filesize(AuthHelper::OAuthRefreshTokenPath) > 0) |
| 223 | + { |
| 224 | + $refreshTokenfile = @\fopen(AuthHelper::OAuthRefreshTokenPath,"r"); |
| 225 | + $refreshToken = fread($refreshTokenfile, filesize(AuthHelper::OAuthRefreshTokenPath)); |
| 226 | + fclose($refreshTokenfile); |
| 227 | + } |
| 228 | + |
| 229 | + return $refreshToken; |
| 230 | + } |
| 231 | + |
| 232 | + static function WriteOAuthRefreshToken($refreshToken) |
| 233 | + { |
| 234 | + $refreshTokenfile = @\fopen(AuthHelper::OAuthRefreshTokenPath,"wb"); |
| 235 | + if (file_exists(AuthHelper::OAuthRefreshTokenPath)) |
| 236 | + { |
| 237 | + fwrite($refreshTokenfile, $refreshToken); |
| 238 | + fclose($refreshTokenfile); |
| 239 | + } |
| 240 | + |
| 241 | + return; |
| 242 | + } |
| 243 | +} |
0 commit comments