Hello Sir,

I have integrate oauth2 but If anyone user allow oauth service which is not 
link on my adwords account then I get USER_PERMISSION_DENIED error.
If user is link on my adwords account then I get customer id, but only that 
users which is not link in my adword account.


we want to create a tool to link all customer account with a manager 
account which is allow the oauth then get all campaigns/accounts


Please review my code on below

Thanks



<?php
require_once '../vendor2/autoload.php';

use Google\Auth\OAuth2;
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\Common\OAuth2TokenBuilder;
use Google\AdsApi\AdWords;
use Google\AdsApi\AdWords\v201806\cm\CampaignService;
use Google\AdsApi\AdWords\v201806\mcm\CustomerService;
session_start();

$oauth2 = new OAuth2([
    'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
    'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
    'redirectUri' => 'http://localhost/adword/test.php',
    'clientId' => 'client_id_here',
    'clientSecret' => 'client_secret_here',
    'scope' => 'https://www.googleapis.com/auth/adwords',
    'access_type' => 'offline',
    'prompt' => 'consent',
]);
 if (!isset($_GET['code'])) {

      $oauth2->setState(sha1(openssl_random_pseudo_bytes(1024)));
      $_SESSION['oauth2state'] = $oauth2->getState();

      $config = [
        'access_type' => 'offline'
      ];
      header('Location: ' . $oauth2->buildFullAuthorizationUri($config));
      exit;
    }

    elseif (empty($_GET['state'])
        || ($_GET['state'] !== $_SESSION['oauth2state'])) {
      unset($_SESSION['oauth2state']);
      exit('Invalid state.');
    } else {
      $oauth2->setCode($_GET['code']);
      $authToken = $oauth2->fetchAuthToken();

      $refresh_token = $authToken['access_token'];
    }


    $session = (new AdWordsSessionBuilder())
        ->fromFile('adsapi_php.ini')
        ->withOAuth2Credential($oauth2)
        ->build();
    
    
    $adWordsServices = new AdWordsServices();

    
    $customerService = $adWordsServices->get($session, 
CustomerService::class);
    $customers = $customerService->getCustomers();
    $customerId = $customers[0]->getCustomerId(); 

    echo $customerId; 

-- 
-- 
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog:
https://googleadsdeveloper.blogspot.com/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

You received this message because you are subscribed to the Google
Groups "AdWords API and Google Ads API Forum" group.
To post to this group, send email to adwords-api@googlegroups.com
To unsubscribe from this group, send email to
adwords-api+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"AdWords API and Google Ads API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to adwords-api+unsubscr...@googlegroups.com.
Visit this group at https://groups.google.com/group/adwords-api.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/adwords-api/82c4f349-41b9-4655-a4fc-0acc54f214ee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
  • How to get client c... ankitthapar80

Reply via email to