googleads / google-ads-php

Google Ads API Client Library for PHP
https://developers.google.com/google-ads/api/docs/client-libs/php
Apache License 2.0
280 stars 260 forks source link

How to set login-customer-id when service is built from configuration, not .ini file #1016

Closed svagers closed 2 months ago

svagers commented 3 months ago

Here's how I init $google Ads client: `$login_customer_id = 0000000000; // Customer id of my test ads manager account

$configuration = new Configuration([
    'GOOGLE_ADS' => [
        'developerToken' => 'production ads manager account developer token',
        'use_proto_plus' => "true"
    ],
    'OAUTH2' => [
        'clientId' => env('GOOGLE_ADS_CLIENT_ID'),
        'clientSecret' => env('GOOGLE_ADS_CLIENT_SECRET'),
        'refreshToken' => auth()->user()->google_refresh_token
    ]
]);

$oAuth2Credential = (new OAuth2TokenBuilder())->from($configuration)->build();

return (new GoogleAdsClientBuilder())->from($configuration)
    ->withOAuth2Credential($oAuth2Credential)
    ->withLoginCustomerId($login_customer_id)
    ->usingGapicV2Source(true)
    ->build();`

I don't use .ini file because I need refresh_token to be dynamic. Either way, that should matter unless the API is strict on its usage for some unreasonable thinking.

Then I issue GenerateKeywordHistoricalMetricsRequest like this: `$service = $client->getKeywordPlanIdeaServiceClient();

$response = $service->generateKeywordHistoricalMetrics(
    new GenerateKeywordHistoricalMetricsRequest([
        'customer_id' => 0000000000, // the client id of the test client I have created on my test ads manager account
        'keywords' => ['test keyword', 'test word'],
        'geo_target_constants' => [ResourceNames::forGeoTargetConstant(2428)],
        'keyword_plan_network' => KeywordPlanNetwork::GOOGLE_SEARCH,
        'language' => ResourceNames::forLanguageConstant(1028)
    ])
);`

I get the following error: { "message": "The caller does not have permission", "code": 7, "status": "PERMISSION_DENIED", "details": [ { "@type": "type.googleapis.com\/google.ads.googleads.v16.errors.GoogleAdsFailure", "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" } ], "requestId": "osV8_KVv9xsqILyn2S4TKQ" } ] }

I have also tried setting the login-customer-id directly in Configuration: $configuration = new Configuration([ 'GOOGLE_ADS' => [ 'developerToken' => 'production ads manager account developer token', 'use_proto_plus' => "true", 'loginCustomerId' => $login_customer_id ], 'OAUTH2' => [ 'clientId' => env('GOOGLE_ADS_CLIENT_ID'), 'clientSecret' => env('GOOGLE_ADS_CLIENT_SECRET'), 'refreshToken' => auth()->user()->google_refresh_token ] ]);

I know that it should work as it throws an error when the client id with hyphens is provided (changed the client id in error message): { "message": "Request contains an invalid argument.", "code": 3, "status": "INVALID_ARGUMENT", "details": [ { "@type": "type.googleapis.com\/google.ads.googleads.v16.errors.GoogleAdsFailure", "errors": [ { "errorCode": { "headerError": "INVALID_LOGIN_CUSTOMER_ID" }, "message": "The login customer id header 'Optional[000-000-0000]' could not be validated." } ], "requestId": "_bWfhbEyhXF0C6NzssoXVQ" } ] }

So, I guess I don't understand why I still get PERMISSION_DENIED error. It says 'Note: If you're accessing a client customer, the manager's customer id must be set in the 'login-customer-id' header.' in the error, so I guess it's just a note, not an actual error. If it is the case, I would suggest removing this note and provide actual case errors instead.

svagers commented 3 months ago

Some notes here:

  1. For some reason my testing Ads Manager Account has a weird status of '[Closed]'since the registration of this account. image

  2. My test Client account's status is 'Canceled' Screenshot 2024-04-26 at 11 31 37

  3. There's an error when I tried creating a new test client: image

fiboknacky commented 3 months ago

Your questions are more related to the account settings and the Google Ads API product in general. Could you please post on the Google Ads API forum?

svagers commented 3 months ago

If it's not an issue with the usage of code library, then sure. My suspicions arose around the fact that the way I configured both Oauth and Client factories is hardly documented anywhere I could find.

fiboknacky commented 3 months ago

Most information you want should be documented here. Feel free to give feedback if you find something is missing. :)

svagers commented 3 months ago

Can you point me to the page in this documentation where it mentions the structure of the array required to be passed to the Configuration when using OAuth and Client Factories? Can't find it

fiboknacky commented 3 months ago

The configuration parsing relies on the parse_ini_file and parse_ini_string. In particular, you can see an example of the ini file here.

You can also see a sample google_ads_php.ini, which shows all the sections that this library uses.

fiboknacky commented 2 months ago

Closing due to inactivity. Feel free to reopen this if you still need help.