Ajax 对Google API python客户端的应用程序引擎调用返回403,需要@oauth_
这应该是一件小事,但我做不出来 我需要从Gae调用Google日历API;因此,我根据谷歌文档和示例设置了所有内容: 我有一个Ajax 对Google API python客户端的应用程序引擎调用返回403,需要@oauth_,ajax,google-app-engine,google-api,google-api-client,google-api-python-client,Ajax,Google App Engine,Google Api,Google Api Client,Google Api Python Client,这应该是一件小事,但我做不出来 我需要从Gae调用Google日历API;因此,我根据谷歌文档和示例设置了所有内容: 我有一个/auth.py: CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json') SCOPES = [ 'https://www.googleapis.com/auth/calendar', ] decorator = appengine.OAuth2Decorato
/auth.py
:
CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')
SCOPES = [
'https://www.googleapis.com/auth/calendar',
]
decorator = appengine.OAuth2DecoratorFromClientSecrets(
filename=CLIENT_SECRETS,
scope=SCOPES,
cache=memcache,
prompt='consent',
)
由main.py
函数调用:
class Landing(webapp2.RequestHandler):
@auth.decorator.oauth_aware
def get(self):
if auth.decorator.has_credentials():
self.redirect('/in')
else:
self.response.out.write('''
etc. {}'''.format(auth.decorator.authorize_url()))
class Main(webapp2.RequestHandler):
@auth.decorator.oauth_required
def get(self):
links = { ... }
render(self, 'base.html', template_values=links)
class Calendar(webapp2.RequestHandler):
@auth.decorator.oauth_required
def get(self):
service = build('calendar', 'v3', http=auth.decorator.http())
api_request = service.events().list(calendarId='primary')
api_response = api_request.execute()
self.response.headers['Content-Type'] = 'application/json; charset=utf-8'
self.response.out.write(json.dumps(api_response, indent=4))
class PutEvent(webapp2.RequestHandler):
@auth.decorator.oauth_required
def post(self):
# ...
# http = httplib2.Http(memcache)
service = build('calendar', 'v3') #, http=http)
api_response = []
for i in json.loads(self.request.get('events')):
# ...
event = { ... } # Google Calendar event
api_request = service.events().insert(calendarId='primary', body=scadenza)
api_response.append(api_request.execute(http=auth.decorator.http()))
self.response.headers['Content-Type'] = 'application/json; charset=utf-8'
self.response.out.write(json.dumps(api_response, indent=4))
正如您所看到的,这是一个由Ajax jQuery调用($.post({{putEvent_url}})、jsonData、函数(数据){console.log(数据);})请求的相当简单的post
我在开发服务器中,使用test@example
user,并且该应用程序被授权访问我个人帐户的谷歌日历
对我来说奇怪的是,任何对Calendar()的调用都能按预期工作,但对PutEvent()的调用最终都会出现错误500
在控制台中查看回溯的结尾:
File "/home/pierpaolo/Devnos/whiterabbit/include/oauth2client/contrib/appengine.py", line 644, in check_oauth
resp = method(request_handler, *args, **kwargs)
File "/home/pierpaolo/Devnos/whiterabbit/main.py", line 211, in post
api_response.append(api_request.execute(http=auth.decorator.http()))
File "/home/pierpaolo/Devnos/whiterabbit/include/oauth2client/_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "/home/pierpaolo/Devnos/whiterabbit/include/googleapiclient/http.py", line 838, in execute
raise HttpError(resp, content, uri=self.uri)
HttpError: <HttpError 403 when requesting https://www.googleapis.com/calendar/v3/calendars/primary/events?alt=json returned "Forbidden">
INFO 2017-01-04 15:13:32,385 module.py:788] default: "POST /api/put/scadenze HTTP/1.1" 500 -
文件“/home/pierpaolo/Devnos/whiterabbit/include/oauth2client/contrib/appengine.py”,第644行,检查oauth
resp=方法(请求处理程序,*args,**kwargs)
文件“/home/pierpaolo/Devnos/whiterabbit/main.py”,第211行,在post中
api\u response.append(api\u request.execute(http=auth.decorator.http())
文件“/home/pierpaolo/Devnos/whiterabbit/include/oauth2client/_helpers.py”,第133行,在位置包装中
已包装退货(*args,**kwargs)
文件“/home/pierpaolo/Devnos/whiterabbit/include/googleapiclient/http.py”,第838行,在execute中
raise HttpError(resp,content,uri=self.uri)
HttpError:
INFO 2017-01-04 15:13:32385 module.py:788]默认值:“POST/api/put/scadenze HTTP/1.1”500-
我无法理解这个问题
HttpError:https://www.googleapis.com/calendar/v3/calendars/primary/events?alt=json 返回“禁止”>
在我看来,我已经授权应用程序访问我的帐户,并且谷歌应用程序引擎装饰程序已经正确地安装到位,以使OAuth2.0符合
编辑:
我想知道我的问题是否与我调用Google Calendar API的方式有关:
HTML/JS GAE/Py
+------------------+
| |
| <form> |
| ...data |
| <\JS/Ajax |
| $.post(...data | --> GAE/main.py
| | @auth.decorator.oauth_required
| | def post(self, data):
+------------------+ event = elaborate(data)
service = build('calendar', 'v3')
api_request = service.events()
.insert(calendarId='primary',
body=event)
api_response = api_request
.execute(auth.decorator
.http())
self.response(api_response)
HTML/JS-GAE/Py
+------------------+
| |
| |
|…数据|
|GAE/main.py
||@auth.decorator.oauth_必需
|| def post(自身、数据):
+------------------+事件=详细说明(数据)
服务=构建('calendar','v3')
api_请求=service.events()
.insert(calendarId='primary',
主体=事件)
api_响应=api_请求
.execute(auth.decorator)
.http())
自我反应(api_反应)
编辑3:
我仔细研究了oauth2client.contrib.appengine,并在这里和那里添加了一些记录器。调试:我认为问题可能出在执行(http=decorator.http())
调用中,但在我的其他处理程序中是一样的!无论是位置、关键字还是将经过身份验证的http放入服务构建都不会改变错误行为
我也看不出位置包装中的第133行“helpers.py”可能造成什么问题
亲爱的各位,关于如何进一步研究的一些提示?
实际上,我可以在同一个RequestHandler中插入Acl和/或插入辅助日历,该RequestHandler通过events()引发禁止的异常。insert()…!我没有足够的声誉发表评论,所以我将其作为一个答案:
首先,再次检查是否已在上启用calandar api
其次,我使用了contacts API,发现一旦您授予访问权限,您就无法再次授予访问权限,除非您首先撤销初始许可。当用户将我的应用程序连接到Google contacts,然后断开连接,然后尝试重新连接时,我遇到了这种情况-第二次重新连接将失败。要检查/撤销,请转到 显然,问题在于尝试插入一个全天事件,其endTimeUnspecified:True
我在google api python客户端GitHub tracker上打开了一个问题:
也许有人会对此进行调查或发布更准确的答案
谢谢大家。谢谢。但我认为403被禁止的原因在某种程度上与我调用api的方式有关:我认为当我将表单提交给python requesthandler时,我丢失了cookie/authorized\u http/credential/whatever,然后将另一个实际调用google api并包含详细数据的请求委托给该函数。我将编辑qu相应地估计。荒谬:实际上,我可以在同一个RequestHandler中插入Acl和/或插入辅助日历,该RequestHandler会引发与events()有关的禁止异常。insert()…!但是,我也不能在APPENGINE应用程序添加的辅助日历中插入事件!!@everyone:相同的代码对userinfo.profile和calendar成功运行(活动列表)…有什么想法吗?