unauthorized_client in GetCampaign.php file after use refresh token from webapplication

236 views
Skip to first unread message

Bezoar Software

unread,
Nov 29, 2017, 4:44:39 AM11/29/17
to AdWords API Forum
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);

Sreelakshmi Sasidharan (AdWords API Team)

unread,
Nov 29, 2017, 10:49:46 AM11/29/17
to AdWords API Forum
Hi,

The '401 Unauthorized' error usually occurs when you are using incorrect OAuth2 credentials from the target accounts perspective. Could you check if you have provided the correct client credentials (client Id, client secret) and the OAuth token? If you are not sure, you could try generating a new refresh token by following the instructions provided here for the web app flow. 

To verify that your credentials are correct, you could also try the CURL request provided below and see if you are able to generate the access tokens. If the credentials are correct, you should get an access token for the below request.
    -d refresh_token=MCC_refresh_token  \
    -d client_id=your-client-id  \
    -d client_secret=your-client-secret  \
    -d grant_type=refresh_token

Please give this a try and let me know how it goes.

Regards,
Sreelakshmi, AdWords API Team.
Reply all
Reply to author
Forward
0 new messages