如何从 Google Ads API 检索数据

问题描述 投票:0回答:1

(1) 在我现在所在的地区,由于防火墙的原因,Google 网站无法访问。但我使用的是 Windows 版 Clash,我可以访问并访问 Google 网站。它在我的浏览器上运行良好。

(2)我正在通过安装在我的 Windows 10 笔记本电脑上的 Jupyter Notebook 使用简单的 Python 函数测试 Google Ads API,如下所示:

import logging
logging.basicConfig(level=logging.INFO)
from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException

  
# Define the main function
def main(client, customer_id):
    ga_service = client.get_service("GoogleAdsService")

    query = """
        SELECT
            campaign.id,
            campaign.name
        FROM campaign
        ORDER BY campaign.id
    """

    # Issues a search request using streaming.
    stream = ga_service.search_stream(customer_id=customer_id, query=query)

    # Iterate through the results and print them
    for batch in stream:
        for row in batch.results:
            print(
                f"Campaign with ID {row.campaign.id} and name "
                f"{row.campaign.name}"
            )

# Load Google Ads client from storage
# Replace "PATH/TO/YOUR/google-ads.yaml" with the actual path to your file
googleads_client = GoogleAdsClient.load_from_storage(path=r"C:\Users\tomxi\google-ads.yaml", version="v15")

# Get customer ID from user input (optional)
# Replace with your desired method of obtaining the customer ID
customer_id = "xxxxxxxxxx"

# Call the main function
try:
    main(googleads_client, customer_id)
except GoogleAdsException as ex:
    print(
        f'Request with ID "{ex.request_id}" failed with status '
        f'"{ex.error.code().name}" and includes the following errors:'
    )
    for error in ex.failure.errors:
        print(f'\tError with message "{error.message}".')
        if error.location:
            for field_path_element in error.location.field_path_elements:
                print(f"\t\tOn field: {field_path_element.field_name}")
    sys.exit(1)

(3)我收到错误:

[TransportError: HTTPSConnectionPool(host='accounts.google.com', port=443): Max retries exceeded with url: /o/oauth2/token (Caused by SSLError(SSLEOFError(8, '\[SSL: UNEXPECTED_EOF_WHILE_READING\] EOF occurred in violation of protocol (_ssl.c:1006)')))][1]

(4)我猜 Google Ads API 采用了某些路线或其他方式,而这些路线或其他方式不受我笔记本电脑上的 Clash for Window 使用或控制。我不知道如何检查和更改它。请帮助我。

(5)这里是上面代码的完整错误描述,请访问。

python ssl https connection google-ads-api
1个回答
0
投票

您正在 colab 上运行脚本。但您正在引用本地计算机上的配置文件。

如果您希望它在 Colab 中工作,您需要安装 Google Drive 并上传任何文件。

但是,我强烈建议不要在 Colab 上托管您的配置文件,因为它包含极其敏感的凭据,包括您的 Google Ads 开发人员密钥和您的个人 Google 帐户刷新/访问令牌。

© www.soinside.com 2019 - 2024. All rights reserved.