Trying to retrieve listAccessibleCustomers but (Missing authorization code)

67 views
Skip to first unread message

Enzo Patriarca

unread,
Sep 25, 2023, 11:48:40 AM9/25/23
to Google Ads API and AdWords API Forum
$parameters = ['access_type' => 'offline'];

$driver = Socialite::driver('google')->with($parameters);
$user = $driver->stateless()->user();
// dd($user);
$accessToken = $user->token;
$authorizationCode = $_GET['code'];

// $this->afterGoogleCallback($user,$authorizationCode,$accessToken);
$clientid = config('values.Client_ID');
$clientSecret = config('values.Client_secret');
$developerToken = config('values.developer_token');
$redirect_uri = config('values.redirect_uri');
$refresh_token = config('values.google_refresh_token');
$array = [
'authorizationCode' => $authorizationCode,
'grant_type' => 'authorization_code',
'access_token' => $accessToken,
'tokenCredentialUri' => 'https://oauth2.googleapis.com/token',
'clientId' => $clientid,
'clientSecret' => $clientSecret,
'redirectUri' => $redirect_uri,
'developerToken' => $developerToken,
'refreshToken' => $refresh_token,
];

try {
$oauth2Credentials = new OAuth2($array);
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile(public_path('google_ads_php.ini'))->build();
$googleAdsClient = (new GoogleAdsClientBuilder())
->fromFile(public_path('google_ads_php.ini'))
->withOAuth2Credential($oAuth2Credential)
->build();
$this->runExample($googleAdsClient,$authorizationCode);
} catch (Exception $e) {
// Handle any exceptions that may occur during OAuth2 or API requests
dd($e);
}

public static function runExample(GoogleAdsClient $googleAdsClient,$authorizationCode){
// dd($authorizationCode);
$customerServiceClient = $googleAdsClient->getCustomerServiceClient();
// dd($customerServiceClient);
// Create an array of optional arguments
$optionalArgs = [];
// Issues a request for listing all accessible customers.
try {
$accessibleCustomers = $customerServiceClient->listAccessibleCustomers();
} catch (Exception $e) {
dd($e);
}
// Create an array to store account names
$accountNames = [];
// Iterates over all accessible customers' resource names.
foreach ($accessibleCustomers->getResourceNames() as $resourceName) {
// Extract the customer ID from the resource name.
$parts = explode('/', $resourceName);
$customerId = end($parts);
$accountNames[]=$customerId;
}

dd($accountNames);
// Dump the array containing account names.
foreach ($accountNames as $id) {
if ($id == 6444085743) {
$query = "SELECT
customer.descriptive_name
FROM customer";

$streamRequest = $googleAdsClient->getGoogleAdsServiceClient()->searchStream($id, $query);
foreach ($streamRequest->iterateAllElements() as $element) {
// Extract data from each result element.
$customerId = $element->getCustomer()->getId();
$descriptiveName = $element->getCustomer()->getDescriptiveName();
// Do something with the data, such as printing or storing it.
// dd("Customer ID: $customerId, Descriptive Name: $descriptiveName");
$contas = self::getCustomerAccounts($googleAdsClient,$customerId);
foreach ($contas as $account) {
$accountId = $account->getClientCustomer();
$accountName = $account->getDescriptiveName();
// Do something with the account data, such as printing or storing it.
dd("Customer ID: $customerId, Account ID: $accountId, Account Name: $accountName");
}
}
}
}

dd($accountNames);
} public static function runExample(GoogleAdsClient $googleAdsClient,$authorizationCode){
// dd($authorizationCode);
$customerServiceClient = $googleAdsClient->getCustomerServiceClient();
// dd($customerServiceClient);
// Create an array of optional arguments
$optionalArgs = [];
// Issues a request for listing all accessible customers.
try {
$accessibleCustomers = $customerServiceClient->listAccessibleCustomers();
} catch (Exception $e) {
dd($e);
}
// Create an array to store account names
$accountNames = [];
// Iterates over all accessible customers' resource names.
foreach ($accessibleCustomers->getResourceNames() as $resourceName) {
// Extract the customer ID from the resource name.
$parts = explode('/', $resourceName);
$customerId = end($parts);
$accountNames[]=$customerId;
}

dd($accountNames);
// Dump the array containing account names.
foreach ($accountNames as $id) {
if ($id == 6444085743) {
$query = "SELECT
customer.descriptive_name
FROM customer";

$streamRequest = $googleAdsClient->getGoogleAdsServiceClient()->searchStream($id, $query);
foreach ($streamRequest->iterateAllElements() as $element) {
// Extract data from each result element.
$customerId = $element->getCustomer()->getId();
$descriptiveName = $element->getCustomer()->getDescriptiveName();
// Do something with the data, such as printing or storing it.
// dd("Customer ID: $customerId, Descriptive Name: $descriptiveName");
$contas = self::getCustomerAccounts($googleAdsClient,$customerId);
foreach ($contas as $account) {
$accountId = $account->getClientCustomer();
$accountName = $account->getDescriptiveName();
// Do something with the account data, such as printing or storing it.
dd("Customer ID: $customerId, Account ID: $accountId, Account Name: $accountName");
}
}
}
}

dd($accountNames);
}




Google Ads API Forum Advisor

unread,
Sep 28, 2023, 7:30:31 AM9/28/23
to enzo.pa...@joinads.me, adwor...@googlegroups.com
Hi,

Thank you for reaching out to the Google Ads API support team.

By reviewing your concern, I understand that you are trying to retrieve listAccessibleCustomers but getting an error message "Missing authorization code".

In order to investigate your issue further, kindly provide us with the complete API logs (request and response with request-id and request header) along with the uncropped UI screenshot generated at your end. If you are using a client library and haven't enabled logging yet, I would request that you enable logging for the specific client library that you are using. You can refer to the guides in Java.NetPHPPythonRuby, or Perl to enable logging at your end. For REST interface requests, you can enable logging via the curl command by using the -i flag.
 
This message is in relation to case "ref:_00D1U1174p._5004Q2p19lU:ref"

Thanks,
 
Google Logo Google Ads API Team


Google Ads API Forum Advisor

unread,
Sep 28, 2023, 7:38:18 AM9/28/23
to enzo.pa...@joinads.me, adwor...@googlegroups.com
Please ignore my previous email.


Hi,

Thank you for reaching out to the Google Ads API support team.

By reviewing your concern, I understand that you are trying to retrieve listAccessibleCustomers but getting an error message "Missing authorization code".

In order to investigate your issue further, kindly provide us with the complete API logs (request and response with request-id and request header) along with the uncropped UI screenshot generated at your end. If you are using a client library and haven't enabled logging yet, I would request that you enable logging for the specific client library that you are using. You can refer to the guides in Java.NetPHPPythonRuby, or Perl to enable logging at your end. For REST interface requests, you can enable logging via the curl command by using the -i flag.

You can send the details via Reply privately to the author option, or direct private reply to this email.
Reply all
Reply to author
Forward
0 new messages