【发布时间】:2020-02-11 15:02:14
【问题描述】:
下面是我用来生成 Admob 网络报告的代码
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
import os
base_path=os.path.dirname(os.path.realpath(__file__))
scopes=['https://www.googleapis.com/auth/admob.report']
key_file_location = base_path+'/config/service_account.json'
credentials = ServiceAccountCredentials.from_json_keyfile_name(key_file_location, scopes)
account_id='accounts/pub-XXXXXXXXXXXXXXXX'
network_report_filter = {
'dateRange': {
'startDate': {'year': 2020, 'month': 1, 'day': 1},
'endDate': {'year': 2020, 'month': 2, 'day': 10}
},
'dimensions': ['DATE', 'APP', 'COUNTRY'],
'metrics': ['CLICKS', 'ESTIMATED_EARNINGS'],
'dimensionFilters': [
{
'dimension': 'COUNTRY',
'matchesAny': {'values': [{'value': 'US', 'value': 'CN'}]}
}
],
'sortConditions': [
{'dimension':'APP', 'order': 'ASCENDING'},
{'metric':'CLICKS', 'order': 'DESCENDING'}
],
'localizationSettings': {
'currencyCode': 'USD',
'languageCode': 'en-US'
}
}
# Build the service object.
admob = build('admob', 'v1', credentials=credentials)
admob._resourceDesc=network_report_filter
accounts=admob.accounts()
network_report=accounts.networkReport().generate(parent=account_id)
data=network_report.execute()
它会抛出以下错误
*** HttpError: https://admob.googleapis.com/v1/accounts/pub-XXXXXXXXXXXXXXXX/networkReport:generate?alt=json 返回“请求缺少所需的身份验证凭据。预期的 OAuth 2 访问令牌、登录 cookie或其他有效的身份验证凭据。请参阅https://developers.google.com/identity/sign-in/web/devconsole-project。">
我已在启用 Admob API 的情况下生成服务帐户凭据。 但无法弄清楚为什么会出现身份验证错误。
【问题讨论】:
标签: python-3.x admob