0

它應該是一個微不足道的工作,但我不能把它拿出來。應用程序引擎調用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異常。

+0

@everyone:相同的代碼與成功userinfo.profile和日曆(events.list)...任何想法? – user2154587

回答

0

我沒有足夠的信譽發表評論,所以我就離開它作爲一個答案:

首先,在https://console.cloud.google.com/apis/dashboard?project=yourproject

其次啓用,你有calandar API仔細檢查。我使用了聯繫人API,發現一旦您授予訪問權限,一旦您無法再獲得訪問權限,除非您先撤銷初始權限。當用戶將我的應用程序連接到Google Contacts時,我遇到了這種情況,然後斷開連接,然後嘗試重新連接 - 第二次重新連接將失敗。要檢查/撤銷,頭https://security.google.com/settings/security/permissions

+0

謝謝。但我認爲403禁止的原因與我稱之爲api的方式有些相關:我認爲我丟失了cookie/authorized_http/credential /無論何時將我的表單提交給python requesthandler,然後委託給該函數實際調用另一個請求谷歌api與闡述的數據。我將相應地編輯該問題。 – user2154587

+0

Absurd:實際上,我可以插入Acl和/或插入一個輔助日曆在同一個RequestHandler中,該事件通過事件()插入Forbidden異常。insert()...!然而,我不能插入我的應用程序APP添加的第二個日曆中的事件! – user2154587

相關問題