googleads-python-lib icon indicating copy to clipboard operation
googleads-python-lib copied to clipboard

GoogleAdsServerFault : AuthenticationError NETWORK_CODE_REQUIRED

Open emiguevara opened this issue 7 years ago • 5 comments

Just began testing this library, apologies if this is a non-issue.

I have followed the AdManager authorization guidelines using a JSON keyfile and apparently, the authorisation is done correctly.

However, when trying to use the ad_manager_client, I am hitting GoogleAdsServerFault : AuthenticationError NETWORK_CODE_REQUIRED.

Any pointers?

  • Environment: Python 3.7
  • How to reproduce the error:
from googleads import ad_manager
from googleads import oauth2

# OAuth2 credential, prvate key
KEY_FILE = 'my_auth_key.json'
# Ad Manager API information.
APPLICATION_NAME = 'MyTestApp'

# auth
oauth2_client = oauth2.GoogleServiceAccountClient(KEY_FILE, oauth2.GetAPIScope('ad_manager'))
ad_manager_client = ad_manager.AdManagerClient(oauth2_client, APPLICATION_NAME)

# test client object
for network in ad_manager_client.GetService('NetworkService').getAllNetworks():
    print(network)

# init line item service
line_item_service = ad_manager_client.GetService('LineItemService', version='v201902')
statement = ad_manager.StatementBuilder(version='v201902')

# fetch
response = line_item_service.getLineItemsByStatement(statement.ToStatement())
## Output
{
    'id': 490376,
    'displayName': '....', # good name returned
    'networkCode': '...',  # good code returned
    'propertyCode': '....', # good code returned
    'timeZone': 'Europe/Oslo',
    'currencyCode': 'NOK',
    'secondaryCurrencyCodes': [],
    'effectiveRootAdUnitId': '...', # ok
    'isTest': False
}
Error summary: {'faultMessage': '[AuthenticationError.NETWORK_CODE_REQUIRED @ ]', 'requestId': 'd301f0a89704e7b8dd62bd0533ae24b6', 'responseTime': '31', 'serviceName': 'LineItemService', 'methodName': 'getLineItemsByStatement'}
---------------------------------------------------------------------------
Fault                                     Traceback (most recent call last)
~/Development/google_admanager/env/lib/python3.7/site-packages/googleads/common.py in MakeSoapRequest(*args)
   1381         return soap_service_method(
-> 1382             *packed_args, _soapheaders=soap_headers)['body']['rval']
   1383       except zeep.exceptions.Fault as e:

~/Development/google_admanager/env/lib/python3.7/site-packages/zeep/proxy.py in __call__(self, *args, **kwargs)
     41             self._proxy._client, self._proxy._binding_options,
---> 42             self._op_name, args, kwargs)

~/Development/google_admanager/env/lib/python3.7/site-packages/zeep/wsdl/bindings/soap.py in send(self, client, options, operation, args, kwargs)
    131 
--> 132         return self.process_reply(client, operation_obj, response)

~/Development/google_admanager/env/lib/python3.7/site-packages/zeep/wsdl/bindings/soap.py in process_reply(self, client, operation, response)
    193         if response.status_code != 200 or fault_node is not None:
--> 194             return self.process_error(doc, operation)

~/Development/google_admanager/env/lib/python3.7/site-packages/zeep/wsdl/bindings/soap.py in process_error(self, doc, operation)
    298             actor=get_text('faultactor'),
--> 299             detail=fault_node.find('detail'))

Fault: [AuthenticationError.NETWORK_CODE_REQUIRED @ ]

During handling of the above exception, another exception occurred:

GoogleAdsServerFault                      Traceback (most recent call last)
<ipython-input-1-999715fa8a1b> in <module>
     20 
     21 # fetch
---> 22 response = line_item_service.getLineItemsByStatement(statement.ToStatement())

~/Development/google_admanager/env/lib/python3.7/site-packages/googleads/common.py in MakeSoapRequest(*args)
   1392           error_list = fault.errors or error_list
   1393         raise googleads.errors.GoogleAdsServerFault(
-> 1394             e.detail, errors=error_list, message=e.message)
   1395     return MakeSoapRequest

GoogleAdsServerFault: [AuthenticationError.NETWORK_CODE_REQUIRED @ ]

emiguevara avatar May 10 '19 11:05 emiguevara

I got around this issue as follows:

  • first connect with a JSON key
  • copy the network_code value from ad_manager_client.GetService('NetworkService').getAllNetworks()
  • create a googleads.yaml file and write my application_name, path_to_private_key_file and the obtained network_code
  • from then on, use the googleads.yaml file to connect, do not use the JSON key directly

A bit of a circular process, and contradicting the class pydocs where network_code is described as optional. But it seems to work.

emiguevara avatar May 10 '19 12:05 emiguevara

Network code is technically optional in the sense that you don't need it for the getAllNetworks API call. But you do need it for every other API call as they're scoped to a single network.

christopherseeley avatar Jul 15 '19 19:07 christopherseeley

I had this same issue. Add network code to client call
ad_manager_client = ad_manager.AdManagerClient(oauth2_client, APPLICATION_NAME, network_code = '1111111') to avoid using yaml file.

More info about client arguments here

poorniaxios avatar Aug 28 '19 17:08 poorniaxios

Did you tried following these steps to get the network code? https://support.google.com/admanager/answer/7674889?hl=en

tn-marcos-pajon avatar Nov 28 '19 13:11 tn-marcos-pajon

Im facing the same issue , when I try to just print the current network code . below:

from googleads import ad_manager
import _locale
_locale._getdefaultlocale = (lambda *args: ['en_US', 'UTF-8'])

# Initialize a client object, by default uses the credentials in ~/googleads.yaml.
client = ad_manager.AdManagerClient.LoadFromStorage()

# Initialize a service.
network_service = client.GetService('NetworkService', version='v202205')

# Make a request.
current_network = network_service.getCurrentNetwork()

print("Current network has network code '%s' and display name '%s'." %
        (current_network['networkCode'], current_network['displayName']))

googleadsyaml file has network code and the path to json file in it.

The error message says :

Error summary: {'faultMessage': '[AuthenticationError.NETWORK_CODE_REQUIRED @ ]', 'requestId': '2d1f2be3a75136da84034826fe494301', 'responseTime': '22', 'serviceName': 'NetworkService', 'methodName': 'getCurrentNetwork'}

Lakshmi-CG avatar Jun 21 '22 11:06 Lakshmi-CG