Hello Adword Team,
I want to use webapp flow for get all users info when they visit my site. When i use Console for generating refresh token GetCampaign return all campaign but after generate refresh token using outh from browser it gives me error Uncaught exception 'GuzzleHttp\Exception\ClientException' with message
'Client error: `POST
https://www.googleapis.com/oauth2/v4/token`
resulted in a `401 Unauthorized` response:
{
"error": "unauthorized_client",
"error_description": "Unauthorized"
}
Please help me to built application. My code is
$oauth2 = new OAuth2([
'authorizationUri' => '
https://accounts.google.com/o/oauth2/v2/auth',
'tokenCredentialUri' => '
https://www.googleapis.com/oauth2/v4/token',
'redirectUri' => 'REDIRECT_URL',
'clientId' => 'CLIENT_ID',
'clientSecret' => 'CLIENT_SECRET',
'scope' => '
https://www.googleapis.com/auth/adwords'
]);
$callbackUrl = "CALLBACK_URL";
if (!isset($_GET['code'])) {
$oauth2->setState(sha1(openssl_random_pseudo_bytes(1024)));
$_SESSION['oauth2state'] = $oauth2->getState();
$config = [
// Set to 'offline' if you require offline access.
'access_type' => 'offline',
'approval_prompt'=>'force'
];
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();
echo '<pre>';
print_r($authToken);die;
$refreshToken = $authToken['refresh_token'];
$session = (new AdWordsSessionBuilder())
->fromFile('adsapi_php.ini')
->withOAuth2Credential($oauth2)
->build();
/* ------------------------- Session build ---------------------------------------------*/
/* -------------------------------- Adwords Services section ----------------------------*/
/* Creating object of Adwords services */
$adWordsServices = new AdWordsServices();
/* Adwords Customer services */
$customerService = $adWordsServices->get($session, CustomerService::class);
$customers = $customerService->getCustomers();
$customerId = $customers[0]->getCustomerId(); // Getting main customer client id
$content .= '[ADWORDS]';
$content .= 'developerToken = "DEVELOPER_TOKEN"';
$content .= 'clientCustomerId = "'.$customerId.'"';
$content .= '[OAUTH2]';
$content .= 'clientId = "CLIENT_ID';
$content .= 'clientSecret = "CLIENT_SECRET"';
$content .= 'refreshToken = "'.$refreshToken.'"';
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/google/nw.ini","wb");
fwrite($fp,$content);
fclose($fp);