谷歌日历API无法正常工作

时间:2015-04-11 00:03:23

标签: python calendar oauth-2.0

我只是想让我的代码从我的日历中获取任何数据。 我正在使用python与google api python客户端访问日历api。

这是主页的代码

#!/usr/bin/python
print "Content-type: text/html\n\n"
from oauth2client.client import flow_from_clientsecrets

FLOW = flow_from_clientsecrets('client_secrets.json',scope='https://www.googleapis.com/auth/calendar',redirect_uri='http://127.0.0.1/cgi-bin/py.py')
authorize_url = FLOW.step1_get_authorize_url()
print '<meta HTTP-EQUIV="REFRESH" content="0; url='+authorize_url+'">'

它所做的就是将其重定向到同意页面。

这是重定向的页面“py.py”

#!/usr/bin/python
print "Content-type: text/html\n\n"
import re
import httplib2,urllib2
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import flow_from_clientsecrets
def getCode(parameters):
    try:
            return re.search('code=(.*)',parameters).group(1)
    except:
            return False
FLOW = flow_from_clientsecrets('client_secrets.json',scope='https://www.googleapis.com/auth/calendar',redirect_uri='http://127.0.0.1/cgi-bin/py.py')

currentUrl=os.environ['REQUEST_URI']
code=getCode(currentUrl)

credentials = FLOW.step2_exchange(code)

http=httplib2.Http()
http=credentials.authorize(http)
calendar = build('calendar', 'v3', http=http)
print calendar.calendarList().list().to_json()

这是最后打印的输出

{"body": null, "resumable_uri": null, "headers": {"accept-encoding": "gzip, deflate", "accept": "application/json", "user-agent": "google-api-python-client/1.3.2 (gzip)"}, "uri": "https://www.googleapis.com/calendar/v3/users/me/calendarList?alt=json", "resumable": null, "methodId": "calendar.calendarList.list", "body_size": 0, "resumable_progress": 0, "method": "GET", "_in_error_state": false, "response_callbacks": []}

我已经在google的开发者页面上阅读了有关Oauth2.0的每个页面和python的日历API。我试着google很多。我已经坚持这个代码一个星期了。

有关calendar.calenderList()。list()方法的信息:

https://developers.google.com/apis-explorer/?hl=en_US#p/calendar/v3/calendar.calendarList.list

0 个答案:

没有答案