/** * Runs the example. * @param string $clientId the OAuth2 client ID * @param string $clientSecret the OAuth2 client secret */ function UseOAuth2Example($clientId, $clientSecret) { // Set the OAuth2 client ID and secret. $oauth2Info = array('client_id' => $clientId, 'client_secret' => $clientSecret); // Create the AdWordsUser and set the OAuth2 info. $user = new AdWordsUser(); $user->SetOAuth2Info($oauth2Info); $user->LogAll(); // Get the authorization URL for the OAuth2 token. // No redirect URL is being used since this is an installed application. A web // application would pass in a redirect URL back to the application, // ensuring it's one that has been configured in the API console. // Passing true for the second parameter ($offline) will provide us a refresh // token which can used be refresh the access token when it expires. $authorizationUrl = $user->GetOAuth2AuthorizationUrl(NULL, TRUE); // In a web application you would redirect the user to the authorization URL // and after approving the token they would be redirected back to the // redirect URL, with the URL parameter "code" added. For desktop // or server applications, spawn a browser to the URL and then have the user // enter the authorization code that is displayed. printf("Log in to your AdWords account and open the following URL: %s\n", $authorizationUrl); print 'After approving the token enter the authorization code here: '; $stdin = fopen('php://stdin', 'r'); $code = trim(fgets($stdin)); fclose($stdin); // Get the access token using the authorization code. Ensure you use the same // redirect URL used when requesting authorization. $user->GetOAuth2AccessToken($code, NULL); // The access token expires but the refresh token obtained for offline use // doesn't, and should be stored for later use. $oauth2Info = $user->GetOAuth2Info(); print "OAuth2 authorization successful.\n"; print_r($oauth2Info); // Get the number of campaigns in the account. $campaignService = $user->GetService('CampaignService', ADWORDS_VERSION); $selector = new Selector(); $selector->fields = array('Id'); $selector->paging = new Paging(0, 0); $page = $campaignService->get($selector); // Display number of campaigns. printf("Found %d campaigns.\n", $page->totalNumEntries); }
/** * Runs the example. */ function UseOAuthExample() { // Set the OAuth consumer key and secret. Anonymous values can be used for // testing, and real values can be obtained by registering your application: // http://code.google.com/apis/accounts/docs/RegistrationForWebAppsAuto.html $oauthInfo = array('oauth_consumer_key' => 'anonymous', 'oauth_consumer_secret' => 'anonymous'); // Create the AdWordsUser and set the OAuth info. $user = new AdWordsUser(); $user->SetOAuthInfo($oauthInfo); $user->LogAll(); // Request a new OAuth token. Web applications should pass in a callback URL // to redirect the user to after authorizing the token. $user->RequestOAuthToken(); // Get the authorization URL for the OAuth token. $authorizationUrl = $user->GetOAuthAuthorizationUrl(); // In a web application you would redirect the user to the authorization URL // and after approving the token they would be redirected back to the // callback URL, with the URL parameter "oauth_verifier" added. For desktop // or server applications, spawn a browser to the URL and then have the user // enter the verification code that is displayed. printf("Log in to your AdWords account and open the following URL: %s\n", $authorizationUrl); print 'After approving the token enter the verification code here: '; $stdin = fopen('php://stdin', 'r'); $verifier = trim(fgets($stdin)); fclose($stdin); // Upgrade the authorized token. $user->UpgradeOAuthToken($verifier); // An upgraded token does not expire and should be stored and reused for // every request to the API. $oauthInfo = $user->GetOAuthInfo(); print "OAuth authorization successful.\n"; print_r($oauthInfo); // Get the number of campaigns in the account. $campaignService = $user->GetService('CampaignService', ADWORDS_VERSION); $selector = new Selector(); $selector->fields = array('Id'); $selector->paging = new Paging(0, 0); $page = $campaignService->get($selector); // Display number of campaigns. printf("Found %d campaigns.\n", $page->totalNumEntries); }
$adGroupAd = new AdGroupAd(); $adGroupAd->adGroupId = $adGroupId; $adGroupAd->ad = $ad; // Update the status. $adGroupAd->status = 'PAUSED'; // Create operation. $operation = new AdGroupAdOperation(); $operation->operand = $adGroupAd; $operation->operator = 'SET'; $operations = array($operation); // Make the mutate request. $result = $adGroupAdService->mutate($operations); // Display result. $adGroupAd = $result->value[0]; printf("Ad of type '%s' with id '%s' has updated status '%s'.\n", $adGroupAd->ad->AdType, $adGroupAd->ad->id, $adGroupAd->status); } // Don't run the example if the file is being included. if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { return; } try { // Get AdWordsUser from credentials in "../auth.ini" // relative to the AdWordsUser.php file's directory. $user = new AdWordsUser(); // Log every SOAP XML request and response. $user->LogAll(); // Run the example. PauseAdExample($user, $adGroupId, $adId); } catch (Exception $e) { printf("An error has occurred: %s\n", $e->getMessage()); }
private function createUser() { $user = new \AdWordsUser(null, $this->config->getDeveloperToken(), $this->config->getUserAgent(), null, null, $this->config->getOauth2Info()); $user->SetClientCustomerId($this->config->getClientCustomerId()); LoggerStreamWrapper::setLogger($this->logger); $handler = LoggerStreamWrapper::register(); \Logger::LogToStream(\Logger::$SOAP_XML_LOG, $handler); \Logger::LogToStream(\Logger::$REQUEST_INFO_LOG, $handler); \Logger::LogToStream(\ReportUtils::$LOG_NAME, $handler); // Log every SOAP XML request and response. $user->LogAll(); return $user; }