HttpError 401“需要登录”同时使用Calendar v3 api python插入事件 [英] HttpError 401 "Login Required" while using Calendar v3 api python to insert event

查看:84
本文介绍了HttpError 401“需要登录”同时使用Calendar v3 api python插入事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用python和appengine创建了一个使用Google Calendar API(V3)的应用程序。

我可以列出来自多个日历的事件等。 ,当尝试将事件插入日历时,我遇到了问题。



如果我在我的处理程序中使用get(),一切都正常(即事件被正确插入,但是如果我使用表单和post()来插入事件(在同一日历中),它会失败并显示以下消息:




 < HttpError 401在请求https://www.googleapis.com/calendar/v3/calendars/.../events?alt=json时返回需要登录> 

追踪(最近呼叫最后):

文件/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py,第1511行,在__call__
rv = self.handle_exception(request,response,e )

文件/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py,行1505,在__call__
中rv = self.router.dispatch(request,response)

文件/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py,第1253行,在default_dispatcher
返回route.handler_adapter(请求,响应)

文件/ python27_ru ntime / python27_lib / versions / third_party / webapp2-2.3 / webapp2.py,第547行,发送
返回self.handle_exception(e,self.app.debug)

文件/ python27_runtime / python27_lib / versions / third_party / webapp2-2.3 / webapp2.py,第545行,发送
返回方法(* args,** kwargs)

文件/ base / data /home/apps/.../oauth2client/appengine.py,第469行,check_oauth
返回方法(request_handler,* args,** kwargs)

文件/ base /数据/家庭/应用程序/ ...,第99行,在
request = service.events()。insert(calendarId ='MyCalendarId',body = event).execute()

文件/base/data/home/apps/.../apiclient/http.py,第678行,执行
提升HttpError(resp,content,uri = self.uri)

HttpError:< HttpError 401请求https://www.googleapis.com/calendar/v3/calendars/.../events?alt=json时返回需要登录>






函数的简化版本get()和post ):

  class AddEvent(webapp2.RequestHandler):
@ decorator.oauth_required
def post (self):
如果decorator.has_credentials():
#event_name = self.request.get('event-name')
event = {
'summary':self .request.get('summary'),
'location':self.request.get('place'),
'status':self.request.get('status'),
'start':{
'dateTime':'2013-05-11T10:00:00.000-07:00',
'timeZone':'America / New_York'
},
'end':{
'dateTime':'2013-05-11T10:25:00.000-07:00',
'timeZone':'America / New_York'
},
}

request = service.events()。insert(calendarId ='MyCalendarId',body = event).execute()

else:
self.response.out.write(json.dumps({错误':'没有凭据')))

page ='main'
template_values = {
'url':url,$ b $'url_linktext':url_linktext,
'menu':page
}

template = jinja_environment.get_template('templates / index.html')
self.response.out.write(template。 render(template_values))

@ decorator.oauth_required
def get(self):$ b $如果decorator.has_credentials():
event_name = self.request.get( 'event-name')
event = {
'summary':'通过get'预约',
'location':'某处靠近它',
'start': {
'dateTime':'2013-04-15T10:00:00.000-07:00',
'timeZone':'America / New_York
',
'end':{
'dateTime':'2013-04-15T10:25:00.000-07:00',
'timeZone':'America / New_York'
},
}

http = decorator.http()

request = service.events()。insert(calendarId ='MyCalendarId ',body = event)
inserted = request.execute(http)

else:
self.response.out.write(json.dumps({'error':'没有凭据'}))

page ='main'
template_values = {
'url':url,
'url_linktext':url_linktext,
'menu':page
}

template = jinja_environment.get_template('templates / index.html')
self.response.out.write(template.render(template_values) )

使用OAuth2如下:

  decorator = OAuth2Decorator(
client_i d ='client ID',
client_secret ='client secret',
scope ='https://www.googleapis.com/auth/calendar')

我无法弄清楚这里有什么问题,为什么我在一种情况下获得需要登录异常,而当两种方法都做得很好时非常相同的事情。



获得任何帮助。

解决方案

发现如何解决问题。



我需要将post()方法更改为如下所示:

  $ b $如果decorator.has_credentials():
#event_name = self.request.get('event-name' )
event = {
'summary':self.request.get('summary'),
'location':self.request.get('place'),
'status':self.request.get('status'),
'start':{$ b $'dateTime':'2013-05-11T10:00:00.000-07:00',
'timeZone':'America / New_York'
},
'end':{
'dateTime':'2013-05-11T10:25:00.000-07:00',
'timeZone':'America / New_York'
},
}
http = decorator.http()
request = service.events()。insert(calendarId ='MyCalendarId',body = event).execute(http = http)


该解决方案实际上基于此答案


I am using python and appengine to create an application that uses Google Calendar API (V3).

I am able to list events, from multiple calendars etc. However, I am running into problems when trying to insert an event into a calendar.

If I use get() in my handler, everything works fine (ie. event is inserted properly. However, if I use a form and post() to insert the event (in the same calendar) it fails with the following message


<HttpError 401 when requesting https://www.googleapis.com/calendar/v3/calendars/.../events?alt=json returned "Login Required">

Traceback (most recent call last):

File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)

File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)

File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
   return route.handler_adapter(request, response)

File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)

File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)

File "/base/data/home/apps/.../oauth2client/appengine.py", line 469, in check_oauth
return method(request_handler, *args, **kwargs)

File "/base/data/home/apps/...", line 99, in post
request = service.events().insert(calendarId='MyCalendarId', body=event).execute()

File "/base/data/home/apps/.../apiclient/http.py", line 678, in execute
    raise HttpError(resp, content, uri=self.uri)

HttpError: <HttpError 401 when requesting https://www.googleapis.com/calendar/v3/calendars/.../events?alt=json returned "Login Required">


Simplified versions of functions get() and post() are below:

class AddEvent(webapp2.RequestHandler):
@decorator.oauth_required
def post(self):
    if decorator.has_credentials():          
        #event_name = self.request.get('event-name')
        event = {  
         'summary': self.request.get('summary'),  
         'location': self.request.get('place'),  
         'status' : self.request.get('status'),
         'start': {    
                   'dateTime': '2013-05-11T10:00:00.000-07:00'  ,
                   'timeZone': 'America/New_York'
                   },  
         'end': {    
                 'dateTime': '2013-05-11T10:25:00.000-07:00',
                 'timeZone': 'America/New_York'  
                 },  
        }

        request = service.events().insert(calendarId='MyCalendarId', body=event).execute()

    else:
        self.response.out.write(json.dumps({'error': 'No credentials'}))

    page = 'main'
    template_values = {
      'url': url,
      'url_linktext': url_linktext,
      'menu' : page    
    }

    template = jinja_environment.get_template('templates/index.html')
    self.response.out.write(template.render(template_values))

@decorator.oauth_required
def get(self):
    if decorator.has_credentials():          
        event_name = self.request.get('event-name')
        event = {  
         'summary': 'Appointment from get',  
         'location': 'Somewhere close to it',  
         'start': {    
                   'dateTime': '2013-04-15T10:00:00.000-07:00'  ,
                   'timeZone': 'America/New_York'
                   },  
         'end': {    
                 'dateTime': '2013-04-15T10:25:00.000-07:00',
                 'timeZone': 'America/New_York'  
                 },  
        }

        http = decorator.http()

        request = service.events().insert(calendarId='MyCalendarId', body=event)
        inserted = request.execute(http)

    else:
        self.response.out.write(json.dumps({'error': 'No credentials'}))

    page = 'main'
    template_values = {
      'url': url,
      'url_linktext': url_linktext,
      'menu' : page    
    }

    template = jinja_environment.get_template('templates/index.html')
    self.response.out.write(template.render(template_values))

The OAuth2 is used as follow:

decorator = OAuth2Decorator(
  client_id='client ID',
  client_secret='client secret',
  scope='https://www.googleapis.com/auth/calendar')

I cannot figure out what is wrong here and why I am getting "Login Required" exception in one case and not in the other when both methods do pretty much the same thing.

Any help is appreciated.

解决方案

Found out how to resolve the problem.

I needed to change post() method to look something like this:

@decorator.oauth_aware    
def post(self):

if decorator.has_credentials():          
    #event_name = self.request.get('event-name')
    event = {  
     'summary': self.request.get('summary'),  
     'location': self.request.get('place'),  
     'status' : self.request.get('status'),
     'start': {    
               'dateTime': '2013-05-11T10:00:00.000-07:00'  ,
               'timeZone': 'America/New_York'
               },  
     'end': {    
             'dateTime': '2013-05-11T10:25:00.000-07:00',
             'timeZone': 'America/New_York'  
             },  
    }
    http = decorator.http()
    request = service.events().insert(calendarId='MyCalendarId', body=event).execute(http=http)
    :
    :

The solution is actually based on this answer.

这篇关于HttpError 401“需要登录”同时使用Calendar v3 api python插入事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆