Home:ALL Converter>How to link adwords account through only one time Authentication using outh2?

How to link adwords account through only one time Authentication using outh2?

Ask Time:2017-06-19T12:47:14         Author:Paras

Json Formatter

I am having trouble to get report using Outh2 Authentication flow using PHP for the AdWords API & need some help. Here is the situation:

I have a client on AdWords that I want to pull reports for through the AdWords API (we have a developer token , Refresh Token for this). Let's say that, in AdWords page, Login prompt open every time for login. I would like to implement report using only one time Outh2 login and like show as third party report :

Below is the following code that I try to get working for one time login :

$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' => '[email protected]',
    '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();
 }  

Below is the following code using that 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);

Author:Paras,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/44622420/how-to-link-adwords-account-through-only-one-time-authentication-using-outh2
yy