googleads / googleads-python-lib

The Python client library for Google's Ads APIs
Apache License 2.0
683 stars 975 forks source link

ConnectionError: HTTPSConnectionPool(host='ads.google.com', port=443): Max retries exceeded with url: #494

Closed hamasamray closed 7 months ago

hamasamray commented 2 years ago

Im not sure how to deal with this error. Any suggestions?

ConnectionError: HTTPSConnectionPool(host='ads.google.com', port=443): Max retries exceeded with url: /apis/ads/publisher/v202108/NetworkService?wsdl (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001E7646FADF0>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed'))

prayagupa commented 2 years ago

I get the similar Failed to establish a new connection error while using Application Default Credentials(ADC) from Google Composer. I'm using GoogleRefreshableOAuth2Client

[2022-03-29 17:13:42,383] {taskinstance.py:1152} ERROR - HTTPSConnectionPool(host='ads.google.com', port=443): Max retries exceeded with url: /apis/ads/publisher/v202202/LineItemService?wsdl (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fa4261cb080>: Failed to establish a new connection: [Errno 110] Connection timed out',))
Traceback (most recent call last):
  File "/opt/python3.6/lib/python3.6/site-packages/urllib3/connection.py", line 170, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "/opt/python3.6/lib/python3.6/site-packages/urllib3/util/connection.py", line 96, in create_connection
    raise err
  File "/opt/python3.6/lib/python3.6/site-packages/urllib3/util/connection.py", line 86, in create_connection
    sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out

During handling of the above exception, another exception occurred:

Here is code to connect to GAM running on Google Composer, example is taken from https://github.com/googleads/googleads-python-lib/blob/master/examples/ad_manager/v202202/line_item_service/get_recently_updated_line_items.py

from datetime import datetime
from datetime import timedelta

from airflow import models
from airflow.operators import python_operator
from googleads import ad_manager
from googleads import oauth2
import pytz
import os
import logging

from oauth2client import client

default_args = {
    'start_date': datetime(2021, 3, 1),
    'retries': 2,
    'retry_delay': timedelta(minutes=5),
}

with models.DAG(
        'gam_order_lineitem_etl_task',
        catchup=False,
        schedule_interval=timedelta(minutes=10),
        default_args=default_args) as dag:

    def fetch_gam_order_lineitem(**kwargs):

        logging.info('composer environment variables: ' + str(os.environ))

        refreshable_ouath = oauth2.GoogleRefreshableOAuth2Client()
        ad_manager_client = ad_manager.AdManagerClient(refreshable_ouath, 'MY-ETL')
        ## /v202202/LineItemService
        line_item_service = ad_manager_client.GetService('LineItemService', version='v202202')

        last_modified = (datetime.now(tz=pytz.timezone('America/New_York')) - timedelta(days=10))

        # Create a statement to select line items.
        statement = (ad_manager.StatementBuilder(version='v202202')
                    .Where('lastModifiedDateTime >= :lastModifiedDateTime')
                    .WithBindVariable('lastModifiedDateTime', last_modified))

        while True:
          response = line_item_service.getLineItemsByStatement(statement.ToStatement())

          if 'results' in response and len(response['results']):
            for line_item in response['results']:
              logging.info('Line item with ID and name " was found.' + str((line_item['id']) +  str(line_item['name'])))
            statement.offset += statement.limit
          else:
            break

        logging.info('Number of items found: ' + str(response['totalResultSetSize']))        

    fetch_gam_order_lineitem_task = python_operator.PythonOperator(
        task_id='gam_order_lineitem_etl_task',
        python_callable=fetch_gam_order_lineitem,
        provide_context=True
    )

    fetch_gam_order_lineitem_task
christopherseeley commented 7 months ago

If this is still an issue, I'd recommend checking proxy environment variables and settings. Feel free to reopen if that doesn't resolve it.