Python 可以将OAUTH 2与Google Reporting API一起使用吗?
我目前正在使用OAuth 1 for auth以及GData和Python的报告API。有没有可能使用OAuth 2,我找不到一个可以这样做的参考?我找不到OAuth 2和报告api的任何参考,但是通过以下GData库的示例(http://code.google.com/p/gdata-python-client/source/browse/#hg%2Fsamples%2Fapps)我能够拼凑起来:Python 可以将OAUTH 2与Google Reporting API一起使用吗?,python,api,oauth,reporting,Python,Api,Oauth,Reporting,我目前正在使用OAuth 1 for auth以及GData和Python的报告API。有没有可能使用OAuth 2,我找不到一个可以这样做的参考?我找不到OAuth 2和报告api的任何参考,但是通过以下GData库的示例(http://code.google.com/p/gdata-python-client/source/browse/#hg%2Fsamples%2Fapps)我能够拼凑起来: #!/usr/bin/python import sys import os import ti
#!/usr/bin/python
import sys
import os
import time
import gdata.gauth
import gdata.client
import httplib2
import oauth2client.file
import oauth2client.tools
REPORTING_URI = 'https://www.google.com/hosted/services/v1.0/reports/ReportingData'
REPORTING_XML_TEMPLATE = '''<?xml version="1.0" encoding="UTF-8"?>
<rest xmlns="google:accounts:rest:protocol"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<type>Report</type>
<domain>%s</domain>
<date>%s</date>
<page>%s</page>
<reportType>daily</reportType>
<reportName>%s</reportName>
</rest>'''
OAUTH2FILENAME = 'oauth_20.dat'
OAUTH2JSONFILE = 'client_secrets.json'
OAUTH2SCOPES = 'https://www.google.com/hosted/services/v1.0/reports/ReportingData'
OAUTH2USERAGENT = 'REPORTING'
CLIENTSOURCE = 'REPORTING'
MISSING_OAUTHJSON_FILE_MESSAGE = """
WARNING: Please configure OAuth 2.0
To continue you will need to populate the client_secrets.json file:
%s
with information from the APIs Console <https://code.google.com/apis/console>.
""" % os.path.join(os.path.dirname(__file__), OAUTH2JSONFILE)
### Reporting
def RunReport (http_object, domain, report=None, date=None):
if date is None:
now = time.time()
report_time = time.gmtime(now)
date = time.strftime("%Y-%m-%d",report_time)
if report is None:
report='accounts'
report_data = RequestReport(http_object,domain=domain,report=report,date=date)
if not report_data:
print 'No report data'
return report_data
def RequestReport (http_object, domain=None, report=None, date=None):
"""Retrieves a report
Args:
domain: string
report: string: accounts, activity, disk_space, email_clients, summary
date: string: YYYY-MM-DD
Returns:
String, the report data
"""
report_data = ''
uri = REPORTING_URI
if not report or report is None:
return report_data
if not date or date is None:
return report_data
if not domain or domain is None:
domain = self.domain
page = 1
while True:
report_xml = REPORTING_XML_TEMPLATE %(domain, date, page, report)
response = ''
report_page = ''
try:
response, report_page = http_object.request(
uri,method='POST',body=report_xml)
except Exception, rexcept:
print 'Exception: ',rexcept
report_page = ''
break
if response.status != 200:
print 'Error: ',response.status
report_page = ''
break
if not report_page or report_page == 'End-Of-Report':
break
else:
report_data += report_page
page = page + 1
return report_data
scopes = OAUTH2SCOPES
user_agent = OAUTH2USERAGENT
client_source = CLIENTSOURCE
str_oauth2file = OAUTH2FILENAME
str_oauthjsonfile = OAUTH2JSONFILE
domain = 'somedomain'
report_name = 'accounts'
client_id = 'string'
client_secret = 'string'
report_data = ''
oauth2_flow = ''
now = time.time()
report_time = time.gmtime(now)
report_date = time.strftime("%Y-%m-%d",report_time)
if not os.path.isfile(str_oauth2file):
token = gdata.gauth.OAuth2Token(client_id=client_id,
client_secret=client_secret, scope=scopes, user_agent=user_agent)
uri = token.generate_authorize_url()
print 'Please visit this URL to authorize the application:'
print uri
# Get the verification code from the standard input.
code = raw_input('What is the verification code? ').strip()
token.get_access_token(code)
oauth2_flow = oauth2client.client.flow_from_clientsecrets(str_oauthjsonfile,
scope=scopes,message=MISSING_OAUTHJSON_FILE_MESSAGE)
storage = oauth2client.file.Storage(str_oauth2file)
oauth2_credentials = storage.get()
if oauth2_credentials is None or oauth2_credentials.invalid:
if not oauth2_flow:
oauth2_flow = oauth2client.client.flow_from_clientsecrets(str_oauthjsonfile,
scope=scopes,message=MISSING_OAUTHJSON_FILE_MESSAGE)
print '\nYou must authorize access to the request APIS.\n'
# Save the credentials in storage to be used in subsequent runs.
oauth2_credentials = oauth2client.tools.run(oauth2_flow, storage)
http_oauth2_object = httplib2.Http()
http_oauth2_object = oauth2_credentials.authorize(http_oauth2_object)
report_data = RunReport(
http_oauth2_object,domain,report=report_name,date=report_date)
if report_data:
print report_data
sys.exit(0)
#/usr/bin/python
导入系统
导入操作系统
导入时间
导入gdata.gauth
导入gdata.client
导入httplib2
导入oauth2client.file
导入oauth2client.tools
报告https://www.google.com/hosted/services/v1.0/reports/ReportingData'
报告XML模板=“”
汇报
%
%
%
每日的
%
'''
OAUTH2FILENAME='oauth_20.dat'
OAUTH2JSONFILE='client_secrets.json'
OAUTH2SCOPEShttps://www.google.com/hosted/services/v1.0/reports/ReportingData'
OAUTH2USERAGENT='报告'
CLIENTSOURCE='报告'
缺少\u OAUTHJSON\u文件\u消息“”
警告:请配置OAuth 2.0
要继续,您需要填充client_secrets.json文件:
%
使用来自API控制台的信息。
“%os.path.join(os.path.dirname(__文件),OAUTH2JSONFILE)
###报告
def RunReport(http_对象,域,报告=None,日期=None):
如果日期为无:
now=time.time()
报告时间=time.gmtime(现在)
日期=时间。strftime(“%Y-%m-%d”,报告时间)
如果报告为无:
报告账目
报告\数据=请求报告(http\ U对象,域=域,报告=报告,日期=日期)
如果未报告数据:
打印“无报告数据”
返回报表数据
def RequestReport(http_对象,域=无,报告=无,日期=无):
“”检索报告
Args:
域:字符串
报告:字符串:帐户、活动、磁盘空间、电子邮件客户端、摘要
日期:字符串:YYYY-MM-DD
返回:
字符串,报告数据
"""
报告数据=“”
uri=报告的uri
如果未报告或报告为无:
返回报表数据
如果不是日期或日期为无:
返回报表数据
如果不是域或域为无:
domain=self.domain
页码=1
尽管如此:
报告xml=报告xml模板%(域、日期、页面、报告)
响应=“”
报告页面=“”
尝试:
响应,报告页面=http\u object.request(
uri,method='POST',body=report\u xml)
除例外情况外,rexcept:
打印“异常:”,rexcept
报告页面=“”
打破
如果响应.status!=200:
打印“错误:”,response.status
报告页面=“”
打破
如果不是报告页面或报告页面=='报告结束':
打破
其他:
报告数据+=报告页面
第页=第+1页
返回报表数据
范围=OAUTH2SCOPES
user\u agent=OAUTH2USERAGENT
client\u source=CLIENTSOURCE
str_oauth2file=OAUTH2FILENAME
str_oauthjsonfile=OAUTH2JSONFILE
domain='somedomain'
报告名称='帐户'
客户端id='string'
client_secret='string'
报告数据=“”
oauth2_流量=“”
now=time.time()
报告时间=time.gmtime(现在)
报告日期=时间.strftime(%Y-%m-%d),报告时间)
如果不是os.path.isfile(str_oauth2file):
token=gdata.gauth.OAuth2Token(client\u id=client\u id,
客户机密码=客户机密码,作用域=作用域,用户代理=用户代理)
uri=token.generate\u authorize\u url()
打印“请访问此URL以授权应用程序:”
打印uri
#从标准输入中获取验证代码。
代码=原始输入(“验证代码是什么?”).strip()
令牌。获取\u访问\u令牌(代码)
oauth2_flow=oauth2client.client.flow_from_clientsecrets(str_oauthjsonfile,
范围=范围,消息=缺少\u OAUTHJSON\u文件\u消息)
storage=oauth2client.file.storage(str_oauth2file)
oauth2_凭据=存储。获取()
如果oauth2_凭据为无或oauth2_凭据无效:
如果不是oauth2_流:
oauth2_flow=oauth2client.client.flow_from_clientsecrets(str_oauthjsonfile,
范围=范围,消息=缺少\u OAUTHJSON\u文件\u消息)
打印“\n您必须授权访问请求API。\n”
#将凭据保存在存储器中,以便在后续运行中使用。
oauth2_凭证=oauth2client.tools.run(oauth2_流,存储)
http_oauth2_object=httplib2.http()
http_oauth2_object=oauth2_凭证。授权(http_oauth2_object)
报告\数据=运行报告(
http\u oauth2\u对象,域,报告=报告\u名称,日期=报告\u日期)
如果报告数据:
打印报表数据
系统出口(0)
您自己尝试过任何代码吗?