@ peter-hudec
重复使用相同的线程来解决遇到的新问题。我只能在每次登录时发出一个PUT / POST / GET请求。我无法重复使用登录'结果'对象。
我第一次请求获取Google日历中的所有活动。 我检查自己的事件是否存在于Google日历中。 如果它不存在,请将它们添加到Google日历中。
以下代码适用于单独的页面,但不适用于同一页面中的单个登录会话。
provider_name = 'google'
response = make_response()
print 'Response', response
result = authomatic_inst.login(WerkzeugAdapter(request, response), provider_name)
print 'Result', (result)
if result:
if result.user:
# Get user info
result.user.update()
# Talk to Google Calendar API
if result.user.credentials:
response = result.provider.access('https://www.googleapis.com/calendar/v3/calendars/<CALENDARID>/events?key=<YOUR_API_KEY>', method='GET')
if response.status == 200:
items = response.data.get('items', {})
### <code to Check whether event exists in Google Calendar before adding it in>
# IF event not in Google Calendar, add it
body_json = json.dumps(event)
response = result.provider.access('https://www.googleapis.com/calendar/v3/calendars/<CALENDARID>/events?key=<YOUR_API_KEY>', method='POST', body=body_json, headers={'Content-Type':'application/json;charset=UTF-8'})
if response.status == 200:
res = response.data
return json.dumps(res)
下面的老问题:
我正在使用Authomatic来管理OAuth2.0登录。
我按照创作者发布的[answer] [1]。它适用于 YouTube授权,但不适用于Google日历。
http://peterhudec.github.io/authomatic/reference/classes.html#authomatic.Authomatic.access
if result: if result.user: # Get user info result.user.update() # Talk to Google Calendar API if result.user.credentials: response = result.provider.access('https://www.googleapis.com/calendar/v3/calendars/<calendarid>/events?key=<authkey>',
方法= 'GET') 如果response.status == 200: 打印回复
return response
[GET请求] [2],其中包含Google提供的互动示例 Calendar API是:
GET https://www.googleapis.com/calendar/v3/calendars/<CALENDARID>/events?key={YOUR_API_KEY} Authorization: Bearer ya29.BwGoAyc8yqYGzDz3FEPn-_zYU_EFLy0hiQzbv1h9zOnzlJe4dw1q68WNLuW7weJKNjtYYcy3P_AbsA X-JavaScript-User-Agent: Google APIs Explorer
我很确定我正在使用相同的请求,但我收到了错误代码 403禁止?
[1]:https://stackoverflow.com/a/21987075/3583980 [2]: https://developers.google.com/google-apps/calendar/v3/reference/events/list
答案 0 :(得分:1)
authomatic.login()
方法仅为您获取result.user.credentials
中包含的用户的访问令牌。然后,您可以使用凭据访问提供者API:
response = authomatic.access(result.user.credentials, 'https://example.com/api')
# Following uses the code above internally
response = result.provider.access('https://example.com/api')
每次要发出API请求时,都不需要完成登录过程。我建议将API调用移到一些不同的视图:
def login_view(request):
response = HttpResponse()
result = authomatic.login(DjangoAdapter(request, response),
provider_name)
if result:
if result.credentials:
# You can serialize the credentials to move them across requests.
# The best would be to store them to the DB with the user.
request.session['credentials'] = result.credentials.serialize()
return redirect('api-view')
return response
def api_view(request):
serialized_credentials = request.get('credentials')
if serialized_credentials:
# You can deserialize the credentials...
credentials = authomatic.credentials(serialized_credentials)
# ...and update them if they are about to expire soon
# Google access tokens are valid for 6 months
if credentials.expire_soon():
response = credentials.refresh()
if response.status == 200:
request.session['credentials'] = credentials.serialize()
# The access method accepts both serialized and
# deserialized credentials.
response = authomatic.access(credentials, 'https://example.com/api')
您可以阅读有关credentials in the docs的更多信息。还有一个Webapp2 based tutorial可以轻松移植到Django。