How do I get a list of google adwords api?

36 views
Skip to first unread message
Message has been deleted
Message has been deleted

Евгений Шевцов

unread,
Mar 20, 2018, 1:33:46 AM3/20/18
to AdWords API Forum

I installed this library - https://github.com/googleads/googleads-php-lib

I use this code:

function Auth() {
    session_start();

    $oauth2 = new OAuth2([
        'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
        'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
        'redirectUri' => '***************',
        'clientId' => '***************',
        'clientSecret' => '***************',
        'scope' => 'https://www.googleapis.com/auth/adwords'
    ]);
    if (!isset($_GET['code'])) {
        // Create a 'state' token to prevent request forgery.
        // Store it in the session for later validation.
        $oauth2->setState(sha1(openssl_random_pseudo_bytes(1024)));
        $_SESSION['oauth2state'] = $oauth2->getState();

        // Redirect the user to the authorization URL.
        $config = [
            // Set to 'offline' if you require offline access.
            'access_type' => 'offline'
        ];
        header('Location: ' . $oauth2->buildFullAuthorizationUri($config));
        exit;
    }
    elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
        unset($_SESSION['oauth2state']);
        exit('Invalid state.');
    } else {
        $d = $oauth2->setCode($_GET['code']);

        $authToken = $oauth2->fetchAuthToken();
        $oAuth2Credential = (new OAuth2TokenBuilder())
            ->fromFile()
            ->build();
        $session = (new AdWordsSessionBuilder())
            ->fromFile()
            ->withOAuth2Credential($oAuth2Credential)
            ->build();

        $adWordsServices = new AdWordsServices();

        $campaignService = $adWordsServices->get($session, CampaignService::class);
        define('PAGE_LIMIT',500);
        // Create selector.
        $selector = new Selector();
        $selector->setFields(['Id', 'Name']);
        $selector->setOrdering([new OrderBy('Name', SortOrder::ASCENDING)]);
        $selector->setPaging(new Paging(0, PAGE_LIMIT));

        $totalNumEntries = 0;
        do {
            // Make the get request.
            $page = $campaignService->get($selector);

            // Display results.
            if ($page->getEntries() !== null) {
                $totalNumEntries = $page->getTotalNumEntries();
                foreach ($page->getEntries() as $campaign) {
                    printf(
                        "Campaign with ID %d and name '%s' was found.\n",
                        $campaign->getId(),
                        $campaign->getName()
                    );
                }
            }

            // Advance the paging index.
            $selector->getPaging()->setStartIndex(
                $selector->getPaging()->getStartIndex() + PAGE_LIMIT
            );
        } while ($selector->getPaging()->getStartIndex() < $totalNumEntries);

        printf("Number of results found: %d\n", $totalNumEntries);
    }
In response, "Message: [AuthorizationError.USER_PERMISSION_DENIED @; trigger: ']"

Vincent Racaza (AdWords API Team)

unread,
Mar 20, 2018, 3:00:55 AM3/20/18
to AdWords API Forum
Hi,

You may use this guide instead of the one that you linked on how to authenticate in the AdWords API via installed application or web application or service account type. Also, we recommend that you use the property file in storing your OAuth2 credentials instead of setting them per example file.

Once the authentication is already done and you have already used the property file, you can then run this example on getting campaigns. The example is automatically set-up to utilize the property file.

Let me know if any issue persists after doing the suggestion.

Thanks,
Vincent
AdWords API Team
Reply all
Reply to author
Forward
0 new messages