我无法使用适用于AdWords API的PHP的Outh2身份验证流程获取报告。需要一些帮助。情况如下:
我在AdWords上有一个客户,我想通过AdWords API提取报告(我们有一个开发人员令牌,刷新令牌)。我们可以说,在AdWords页面中,每次登录都会打开登录提示。我想只使用一次Outh2登录实现报告,并像第三方报告那样显示:
以下是我尝试一次登录的代码:
$oauth2 = new OAuth2([
'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
'redirectUri' => 'http://XXXXXXXX/XXXXX',
'clientId' => '1XXXXXXXX-XXXXXXXXX',
'clientSecret' => 'XXXXXXXXXXXXXXXX',
'scope' => 'https://www.googleapis.com/auth/adwords',
'refresh_token'=>'4/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'username' => 'XXXXX.XXXXXXX.XX@gmail.com',
'password'=>'XXXXXXXXX']);
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;
} else if (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state']))
{
unset($_SESSION['oauth2state']);
exit('Invalid state.');
}else
{
$oauth2->setCode($_GET['code']);
$authToken = $oauth2->fetchAuthToken();
$session = (new AdWordsSessionBuilder())
->fromFile()
->withOAuth2Credential($oauth2)
->build();
}
以下是使用I Generate Report的代码:
$selector2 = new Selector();
$selector2->setFields(['AllConversionRate','AccountCurrencyCode','AveragePosition','Clicks','Impressions','Ctr','AverageCpc', 'Cost', 'Conversions','Query','Date']);
$reportDefinition2= new ReportDefinition(); // Create report definition.
$reportDefinition2->setSelector($selector2);
$reportDefinition2->setReportName('Criteria performance report #'.uniqid());
$reportDefinition2->setDateRangeType(ReportDefinitionDateRangeType::LAST_7_DAYS);
$reportDefinition2->setReportType(ReportDefinitionReportType::SEARCH_QUERY_PERFORMANCE_REPORT); // report type
$reportDefinition2->setDownloadFormat(DownloadFormat::CSV);
$reportDownloader2 = new ReportDownloader($session); // Download report.
$reportDownloadResult2 = $reportDownloader2->downloadReport($reportDefinition2);
$reportAsString2 = $reportDownloadResult2->getAsString();
print_r($reportAsString2);
答案 0 :(得分:1)
首先要生成Adwords Performance repoting离线访问,您必须使用" refress token"
生成离线会话Please follow these steps :-
Step 1. Built Offline credentials
public function credentialBuilder($refresstoken) {
$refresh_token = trim($refresstoken);
$configIniFilePath = "adsapi_php.ini"; // place adsapi_ini on root (set correct path)
/* ------ set client credentials ---------------- */
$oauth2_clientId = CLIENT_ID; // set Client Id
$oauth2_clientSecret = CLIENT_SECRET; // set client secret
$refresh_token = $refresh_token; // set refress token get after oauth2 verification
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile(self::$configIniFilePath);
$oAuth2Credential->withClientId($oauth2_clientId);
$oAuth2Credential->withClientSecret($oauth2_clientSecret);
$oAuth2Credential->withRefreshToken($refresh_token);
$credential = $oAuth2Credential->build();
return $credential; // return credentials
}
Step 2. Built Report setting
private function reportSettings() {
$reportSettings = (new ReportSettingsBuilder())
->fromFile(self::$configIniFilePath)
->includeZeroImpressions(false)
->build();
return $reportSettings;
}
Step 3. Generate Report session
public function reportSession($credential = null, $customerId = null) {
$reportSettings = $this->reportSettings(); // get report setting from function
$session = (new AdWordsSessionBuilder())
->fromFile(self::$configIniFilePath)
->withOAuth2Credential($credential) // set credentials using credentialBuilder() function
->withClientCustomerId($customerId) // set client customer ID
->withReportSettings($reportSettings)
->build();
return $session;
}
// Function for get campaign reporting
public function campaignReporting() {
$credential = $this->credentialBuilder();
$session = $obj->reportSession($credential, "Sub_customer_client_id"); // set client id
$reportQuery = 'SELECT CampaignId,CampaignName,Impressions,Clicks,Cost,Ctr,
AverageCost,Conversions,CostPerConversion,ConversionRate,ViewThroughConversions,AverageCpc,AveragePosition
FROM CAMPAIGN_PERFORMANCE_REPORT DURING YESTERDAY';
// Download report as a string.
$reportDownloader = new ReportDownloader($session);
$reportDownloadResult = $reportDownloader->downloadReportWithAwql($reportQuery, 'CSV');
$csvData = $reportDownloadResult->getAsString(); // generate report as string
echo "<pre>";
print_r($csvData);
}