Я пытаюсь получить данные / отчет ("учетная запись") из Google Search Search API Google. https://developers.google.com/search-ads/v2/reference/reports/generate
Однако я получаю эту ошибку.
AttributeError: 'Resource' object has no attribute 'insert'
Я довольно новичок в использовании API. Ниже моя попытка как это
import os
from googleapiclient import discovery
from oauth2client.service_account import ServiceAccountCredentials
import httplib2
from oauth2client import client
# The rest api used to authenticate against.
API_NAME = 'doubleclicksearch'
# The version
API_VERSION = 'v2'
# The OAuth 2.0 scopes to request.
OAUTH_SCOPES = 'https://www.googleapis.com/auth/doubleclicksearch'.split(",")
SERVICE_ACCOUNT_FILE = './client_secrets.json'
def generate_service(dry_run: bool = False) -> discovery.build:
"""
Return an authenticate service.
:param dry_run: Whether it is a test run.
:return: Google API service
"""
if dry_run:
return None
# Authenticate using the supplied service account credentials
http = authenticate_using_service_account(SERVICE_ACCOUNT_FILE)
return discovery.build(API_NAME, API_VERSION, http=http,
cache_discovery=False)
def authenticate_using_service_account(path_to_service_account_json_file: str):
"""
Authorize an http2.Http instance using service accountcredentials.
:param path_to_service_account_json_file: Path of service account.
:return: Authenticated http2.Http instance.
"""
# Load the service account credentials from the specified JSON keyfile.
credentials = ServiceAccountCredentials.from_json_keyfile_name(
path_to_service_account_json_file, scopes=OAUTH_SCOPES)
# Use the credentials to authorize an httplib2.Http instance.
http = credentials.authorize(httplib2.Http())
return http
def create_report(report_name: str, criteria: dict,
dry_run: bool = False) -> dict:
"""
Create CSV Standard report containing the passed criteria.
:param report_name: Name of the report.
:param criteria: Dimensions and metrics asssociated with the report
:param dry_run: Whether it is a test run.
:return: report payload.
"""
service = generate_service(dry_run)
report = dict(name=report_name, type='account',
fileName=f"{report_name}_file", format='CSV',
criteria=criteria)
try:
service.reports().insert(body=report).execute()
except client.AccessTokenRefreshError as auth_err:
logging.critical("The credentials have been revoked or expired")
raise auth_err
logger.info(f"Created report {report_name}")
return report
create_report(report_name="account", criteria= {}, dry_run=False)