我正在使用python从yahoo gemini api检索自定义报告。使用oauth,我可以从API检索报告和支持信息。但是,在包含我请求的字段的报告中,没有任何信息。当报告应返回从我选择的日期(从昨天到昨天的一周)中选择的列的信息时,该报告包含0行。另外,该报告两次返回这些字段,我也不知道为什么这么做。有人可以给我一些有关我做错了什么以及我可以解决的问题以使报告返回实际信息的见解吗?谢谢
我尝试了多种方法来检索从我的OAUTH调用返回的JSON报告。
import time
import urllib.request
import json
import requests
from datetime import datetime as dt, timedelta as td
from yahoo_oauth import OAuth2
#set dates (end date is yesterday, start date is one week before yesterday)
yesterday = (dt.today()-td(1)).strftime('%Y-%m-%d')
start_date = (dt.today()-td(7)).strftime('%Y-%m-%d')
oauth = OAuth2(None, None, from_file='yahoo_credentials/yahooads.json')
if not oauth.token_is_valid():
oauth.refresh_access_token()
response = oauth.session.get("https://api.gemini.yahoo.com/v3/rest/advertiser/")
data = response.content.decode("utf-8")
jdata = json.loads(data)
for j in jdata['response']:
print("Account name: ", j['advertiserName'])
print("Advertiser id: ", j['id'])
print('\n' * 2)
advertiser_id = j['id']
#define payload with chosen fields to return
payload = { "cube": "keyword_stats",
"fields": [
{ "field": "Advertiser ID" },
{ "field": "Day" },
{ "field": "Clicks" },
{ "field": "Device Type" },
{ "field": "Conversions" }
],
"filters": [
{ "field": "Advertiser ID", "operator": "=", "value": advertiser_id},
{ "field": "Day", "operator": "between", "from": start_date, "to": yesterday}
]
}
response = oauth.session.post("https://api.gemini.yahoo.com/v2/rest/reports/custom?reportFormat=json", json=payload)
print(response.content)
print('\n' * 2)
jdata = json.loads(response.content)
job_id = jdata['response']['jobId']
#wait for the report to generate
time.sleep(30)
url = "https://api.gemini.yahoo.com/v2/rest/reports/custom/{}?advertiserId={}".format(job_id, advertiser_id)
response = oauth.session.get(url)
print(response.content)
print('\n' * 2)
rdata = json.loads(response.content)
if 'status' in rdata['response'] and rdata['response']['status'] == 'completed':
report = requests.get(rdata['response']['jobResponse'])
jreport = report.json()
print(jreport)
print('\n' *2)
来自print(jreport),这是我得到的响应:
{'header':
{'cube': 'keyword_stats',
'fields': [
{'fieldName': 'Advertiser ID', 'fieldType': 'DIM'},
{'fieldName': 'Day', 'fieldType': 'DIM'},
{'fieldName': 'Clicks', 'fieldType': 'FACT'},
{'fieldName': 'Device Type', 'fieldType': 'FACT'},
{'fieldName': 'Conversions', 'fieldType': 'FACT'}]},
'fields': [
{'fieldName': 'Advertiser ID', 'fieldType': 'DIM'},
{'fieldName': 'Day', 'fieldType': 'DIM'},
{'fieldName': 'Clicks', 'fieldType': 'FACT'},
{'fieldName': 'Device Type', 'fieldType': 'FACT'},
{'fieldName': 'Conversions', 'fieldType': 'FACT'}
],
'rows': [],
'rowCount': 0
}
问题出在哪里:
'rows': [],
'rowCount': 0
它应该返回[]中的实际信息,并且行数不应该为0