2016-08-18 54 views

回答

3

试试这个LIB https://github.com/josuebrunel/yahoo-oauth下面

import urllib 
import json 
import time 
from yahoo_oauth import OAuth2 

oauth = OAuth2(None, None, from_file='credentials.json') 

if not oauth.token_is_valid(): 
    oauth.refresh_access_token() 

# get all accounts 
response = oauth.session.get("https://api.admanager.yahoo.com/v1/rest/advertiser/") 
data = response.content 
print data 
jdata = json.loads(data) 
for j in jdata['response']: 
    print "{} {}".format(j['id'], j['advertiserName']) 

# get advertiser data 
advertiser_id = 12345678 
report_date_from = "2016-08-28" 
report_date_to = "2016-08-28" 
payload = {"cube": "performance_stats", 
      "fields": [ 
       {"field": "Day"}, 
       {"field": "Impressions"}, 
       {"field": "Conversions"}, 
       {"field": "Spend"}, 
       {"field": "Campaign ID"} 
      ], 
      "filters": [ 
       {"field": "Advertiser ID", "operator": "=", "value": advertiser_id}, 
       {"field": "Day", "operator": "between", "from": report_date_from, "to": report_date_to} 
      ]} 

response = oauth.session.post("https://api.admanager.yahoo.com/v1/rest/reports/custom?reportFormat=json", json=payload) 
print response.content 

jdata = json.loads(response.content) 
job_id = jdata['response']['jobId'] 

# you will need to add some loop and waits before the report is ready 
time.sleep(60) 

url = "https://api.admanager.yahoo.com/v1/rest/reports/custom/{}?advertiserId={}".format(job_id, advertiser_id) 
response = oauth.session.get(url) 
print response.content 

# report will be returned as url 
rdata = json.loads(response.content) 
if 'status' in rdata['response'] and rdata['response']['status'] == 'completed': 
    report = urllib.urlopen(rdata['response']['jobResponse']).read() 
    print report 
+0

我非常感谢你分享这段代码。它帮助我极大地入门。这个信息是超级利基!再次感谢! – Jarad