php how to specify login-customer-id in the request

1,011 views
Skip to first unread message

ma zhiwei

unread,
Aug 16, 2021, 5:28:49 AM8/16/21
to AdWords API and Google Ads API Forum
Hi, I am using google-ads-api to fetch campaign performance data, but I was stop by the following error.

"message": "The caller does not have permission", "code": 7, "status": "PERMISSION_DENIED"

My PHP code: 
```
$customerId = "Manager ID";
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile("google_ads_php.ini")->build();
$googleAdsClient = (new GoogleAdsClientBuilder())->fromFile("google_ads_php.ini")
->withOAuth2Credential($oAuth2Credential)
->withLoginCustomerId($customerId)
->build();

$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
$customerServiceClient = $googleAdsClient->getCustomerServiceClient();
// Issues a request for listing all accessible customers.
$accessibleCustomers = $customerServiceClient->listAccessibleCustomers();
print 'Total results: ' . count($accessibleCustomers->getResourceNames()) . PHP_EOL;

// Iterates over all accessible customers' resource names and prints them.
foreach ($accessibleCustomers->getResourceNames() as $resourceName) {
printf("Customer resource name: '%s'%s", $resourceName, PHP_EOL);
$customer_id = explode("/", $resourceName)[1];

$query = "SELECT campaign.id, campaign.name, campaign.status FROM campaign ORDER BY campaign.id";
$stream = $googleAdsServiceClient->search($customer_id, $query);
echo json_encode($stream);
foreach ($stream->iterateAllElements() as $googleAdsRow) {
printf(
"Campaign with ID %d and name '%s' was found.%s",
$googleAdsRow->getCampaign()->getId(),
$googleAdsRow->getCampaign()->getName(),
PHP_EOL
);
}
}
```

The error in log: 
```
[2021-08-16T08:07:56.532753+00:00] google-ads.INFO: Request made: Host: "googleads.googleapis.com", Method: "/google.ads.googleads.v6.services.CustomerService/ListAccessibleCustomers", CustomerId: "No customer ID could be extracted from the request", RequestId: "Vq8FGtFRDS9D-Id2aMRSzQ", IsFault: 0, FaultMessage: "None" [2021-08-16T08:07:56.672678+00:00] google-ads.WARNING: Request made: Host: "googleads.googleapis.com", Method: "/google.ads.googleads.v6.services.GoogleAdsService/Search", CustomerId: 3452842817, RequestId: "4waISQvRlXbmTrAbjBSvoQ", IsFault: 1, FaultMessage: "["User doesn't have permission to access customer. Note: If you're accessing a client customer, the manager's customer id must be set in the 'login-customer-id' header. See https:\/\/developers.google.com\/google-ads\/api\/docs\/concepts\/call-structure#cid"]" [2021-08-16T08:07:56.673119+00:00] google-ads.NOTICE: Request ------- Method Name: /google.ads.googleads.v6.services.GoogleAdsService/Search Host: googleads.googleapis.com Headers: { "x-goog-api-client": "gl-php\/7.4.3 gapic\/ gax\/1.7.0 grpc\/1.36.0", "x-goog-request-params": "customer_id=3452842817", "developer-token": "REDACTED" } Request: {"customerId":"3452842817","query":"SELECT campaign.id, campaign.name, campaign.status FROM campaign ORDER BY campaign.id","pageSize":1000,"returnTotalResultsCount":true} Response ------- Headers: { "request-id": "4waISQvRlXbmTrAbjBSvoQ", "date": "Mon, 16 Aug 2021 08:07:56 GMT", "alt-svc": "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000,h3-T051=\":443\"; ma=2592000,h3-Q050=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\"" } Fault ------- Status code: 7 Details: The caller does not have permission Failure: {"errors":[{"errorCode":{"authorizationError":"USER_PERMISSION_DENIED"},"message":"User doesn't have permission to access customer. Note: If you're accessing a client customer, the manager's customer id must be set in the 'login-customer-id' header. See https://developers.google.com/google-ads/api/docs/concepts/call-structure#cid"}]}
```

My question is how to specify login-customer-id in the request by php. And what's reason about this error.

Google Ads API Forum Advisor

unread,
Aug 16, 2021, 6:37:31 AM8/16/21
to mzwmav...@gmail.com, adwor...@googlegroups.com

Hi,

Thanks for reaching out to us.

Upon checking the error logs, it seems that the issue ‘USER_PERMISSION_DENIED’ occurred due to incorrectly setting the 'login-customer-id’ in the request. Note that when a user doesn't have permission to access a customer and you’re accessing a client customer then the manager's customer ID must be set in the 'login-customer-id’ in the header request.

That being said, you may refer to this documentation on how to specify the login-customer-id property in the request properly.

In addition, you should need included if you also have the ads properties file setup that corresponds to the library that you are using (e.g. google_ads_php.ini for PHP), as shown here.

Let us know if you have any further questions.

Regards,

Google Logo
Yasar
Google Ads API Team
 


 

ref:_00D1U1174p._5004Q2LnWda:ref

ma zhiwei

unread,
Aug 16, 2021, 9:54:55 PM8/16/21
to AdWords API and Google Ads API Forum
Hi,
Thanks for your response.

My question is how to specify login-customer-id in the request by php, like this:  header("login-customer-id: 1234567890"); ?

I have specified the login-customer-id when I built googleAdsClient object, like this: 
$googleAdsClient = (new GoogleAdsClientBuilder())->fromFile("google_ads_php.ini")
->withOAuth2Credential($oAuth2Credential)
->withLoginCustomerId($customerId)
->build();

I hope you can tell me the exact php code, thanks a lot.

Google Ads API Forum Advisor

unread,
Aug 18, 2021, 1:22:41 PM8/18/21
to mzwmav...@gmail.com, adwor...@googlegroups.com
Hello,

You can specify the login customer ID in the google_ads_php.ini file. For any PHP client library specific questions, please reach out to the client library owners here.

Regards,
Matt
Google Ads API Team

Google Logo
Matt
Google Ads API Team
 


ref:_00D1U1174p._5004Q2LnWda:ref
Reply all
Reply to author
Forward
0 new messages