API がない Google 販売パートナー パネルからデータを取得しようとしています。
urllib2 と cookiejar を使用して正常にログインでき、安全なデータを表示できますが、必要なデータは AJAX で読み込まれます。
AJAX エンドポイントに直接アクセスしようとすると、302 または 404 リダイレクトが発生します。何か案は?
これが私のコードです:
import cookielib, urllib2, urllib, re, pprint
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [
('User-Agent', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11'),
]
# Define URLs (borrowed this from Google voice script on Github)
login_page_url = 'https://accounts.google.com/ServiceLogin?service=grandcentral'
authenticate_url = 'https://accounts.google.com/ServiceLoginAuth?service=grandcentral'
gv_home_page_url = 'https://www.google.com/voice/#inbox'
# Load sign in page
login_page_contents = opener.open(login_page_url).read()
# Find GALX value
galx_match_obj = re.search(r'name="GALX"\s*value="([^"]+)"', login_page_contents, re.IGNORECASE)
galx_value = galx_match_obj.group(1) if galx_match_obj.group(1) is not None else ''
# Set up login credentials
login_params = urllib.urlencode({
'Email' : "<my username>",
'Passwd' : "<my password>",
'continue' : 'https://www.google.com/voice/account/signin',
'GALX': galx_value
})
# Login
opener.open(authenticate_url, login_params)
# Open GV home page
gv_home_page_contents = opener.open(gv_home_page_url).read()
# Fine _rnr_se value
key = re.search('name="_rnr_se".*?value="(.*?)"', gv_home_page_contents)
if not key:
logged_in = False
else:
logged_in = True
print "Logged in!"
reseller_home = opener.open("https://www.google.com/a/cpanel/<my reseller domain>/Reseller2Customers#Reseller2Customers")
print reseller_home.read() #prints data behind the login, http status code 200
opener.addheaders += [
('Referer', 'https://www.google.com/a/cpanel/<my reseller domain>/Reseller2Customers'),
]
reseller_ajax = opener.open("https://www.google.com/a/cpanel/<my reseller domain>/Reseller/GetSubscriptions?sortField=CREATION_DATE&sortAscending=true&nextToken=&customerDomainPrefix=covision.com").read()
#this gives me error 302 or 404