它應該是一個微不足道的工作,但我不能把它拿出來。應用程序引擎調用Google API python客戶端返回403與@oauth_required
我需要從Gae調用Google Calendar API;因此,我設置都歸結爲每谷歌文檔和示例:
我有一個/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))
正如你可以看到這是一個相當簡單的post
通過Ajax jQuery調用請求($.post('{{ putEvent_url }}', jsonData, function(data){ console.log(data); })
...
我在開發服務器上,使用[email protected]
用戶,並且該應用有權訪問我的個人帳戶的Google日曆。
奇怪的事情對我來說,任何調用日曆()正常工作,但在ERROR 500調用PutEvent()結束。
展望回溯的控制檯結束:
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 -
我無法理解的
HttpError:https://www.googleapis.com/calendar/v3/calendars/primary/事件?alt = json返回「Forbidden」>
它在我看來我已經授予應用程序訪問我的帳戶,並且Google App Engine裝飾器已正確放置到使OAuth2.0的東西按照https://developers.google.com/api-client-library/python/guide/google_app_engine ...
編輯: 我在想,如果我的麻煩可與我叫谷歌日曆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)
編輯3: 我查了一下到oauth2client .contrib.appengine和我在這裏和那裏添加了一些logger.debug
:我認爲問題可能在execute(http=decorator.http())
調用中,但它在我的其他處理程序中是一樣的!無論是位置和關鍵字,也沒有把authrized的Http服務build
改變不良行爲......
我也不能看到什麼問題,可能會造成_helpers.py", line 133, in positional_wrapper
...
親愛的朋友們,對一些提示如何進一步考證?實際上,我可以插入Acl和/或在同一個RequestHandler中插入一個輔助日曆,該事件使用events()。insert()...引發Forbidden異常。
@everyone:相同的代碼與成功userinfo.profile和日曆(events.list)...任何想法? – user2154587