我希望每个用户都可以从我们的网站获取他们的Google adwords广告系列数据。 所以在第一步中,我从google api生成了具有用户id的授权URL。 然后,我从谷歌获取代码并生成access_token,在每个用户的db中存储refresh_token。 第二步,我从refresh_token创建了AdwordsSession,并尝试使用此会话来获取每个用户的广告系列数据。 但在这种情况下,我得到了permission_denied错误。 我怎么解决这个问题?
生成授权网址的代码在
之下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();
然后,从auth url获取数据的代码片段如下所示。
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());
在这种情况下,我可以成功获取auth url,代码和令牌,会话,bu无法从下一步获取数据。 生成的错误的代码片段如下。
$customerService = $adWordsServices->get($session, CustomerService::class);
$page = $customerService->getCustomers();