I want that every users can get their google adwords campaign data from our site. So in first step, I generated authorization url with user's id from google api. And then, I got code from google and generate access_token, stored refresh_token in db per user. And in second step, I created AdwordsSession from refresh_token, and tried to use this session to get each user's campaign datas. But in this case, I got permission_denied errors. How can I solve this?
The code of generating authorization url is below
require_once 'vendor/autoload.php';
use Google\Auth\CredentialsLoader;
use Google\Auth\OAuth2;
require_once('../credintal.php');
$callbackUrl = "http://networks.ezond.com/adwords/callback.php";
$scopes = "https://www.googleapis.com/auth/adwords https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/plus.me";
$AUTHORIZATION_URI = 'https://accounts.google.com/o/oauth2/v2/auth';
$oauth2 = new OAuth2([
'authorizationUri' => $AUTHORIZATION_URI,
'redirectUri' => $callbackUrl,
'tokenCredentialUri' => CredentialsLoader::TOKEN_CREDENTIAL_URI,
'clientId' => $googleClientID,
'clientSecret' => $googleClientSecret,
'scope' => $scopes,
'state' => $userID
]);
$authUrl = $oauth2->buildFullAuthorizationUri();
And then, the code snippet to get data from auth url is below.
require_once 'vendor/autoload.php';
use Google\Auth\CredentialsLoader;
use Google\Auth\OAuth2;
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\ReportSettings;
use Google\AdsApi\AdWords\ReportSettingsBuilder;
use Google\AdsApi\AdWords\Reporting\v201705\DownloadFormat;
use Google\AdsApi\AdWords\Reporting\v201705\ReportDefinition;
use Google\AdsApi\AdWords\Reporting\v201705\ReportDefinitionDateRangeType;
use Google\AdsApi\AdWords\Reporting\v201705\ReportDownloader;
use Google\AdsApi\AdWords\v201705\cm\ApiException;
use Google\AdsApi\AdWords\v201705\cm\Paging;
use Google\AdsApi\AdWords\v201705\cm\Predicate;
use Google\AdsApi\AdWords\v201705\cm\PredicateOperator;
use Google\AdsApi\AdWords\v201705\cm\ReportDefinitionReportType;
use Google\AdsApi\AdWords\v201705\cm\Selector;
use Google\AdsApi\AdWords\v201705\cm\OrderBy;
use Google\AdsApi\AdWords\v201705\cm\SortOrder;
use Google\AdsApi\AdWords\v201705\mcm\ManagedCustomerService;
use Google\AdsApi\AdWords\v201705\mcm\CustomerService;
use Google\AdsApi\AdWords\v201705\cm\ReportDefinitionService;
require_once('../credintal.php');
$userID = $_GET["state"];
$userAgent = $googleAppName;
$callbackUrl = "http://networks.ezond.com/adwords/callback.php";
$scopes = "https://www.googleapis.com/auth/adwords https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/plus.me";
$AUTHORIZATION_URI = 'https://accounts.google.com/o/oauth2/v2/auth';
$oauth2 = new OAuth2([
'authorizationUri' => $AUTHORIZATION_URI,
'redirectUri' => $callbackUrl,
'tokenCredentialUri' => CredentialsLoader::TOKEN_CREDENTIAL_URI,
'clientId' => $googleClientID,
'clientSecret' => $googleClientSecret,
'scope' => $scopes,
'approval_prompt' => "force"
]);
$oauth2->setCode($code);
$authToken = $oauth2->fetchAuthToken();
$session = (new AdWordsSessionBuilder())
->fromFile()
->withOAuth2Credential($oauth2)
->build();
$adWordsServices = new AdWordsServices();
$customerService = $adWordsServices->get($session, CustomerService::class);
$page = $customerService->getCustomers();
echo ($page[0]->getCanManageClients());
In this case, I can get auth url, code and token, session successfully, bu can't get data from next step. The code snippet of the error generated is below.
$customerService = $adWordsServices->get($session, CustomerService::class);
$page = $customerService->getCustomers();