简体   繁体   English

获取访问令牌后如何读取用户的Google日历事件?

[英]How to read google calendar events of user after getting access token?

I want to get google calendar events of my users in django and I wrote the following code. 我想在django中获取我的用户的Google日历事件,并编写了以下代码。 This code works and saves access token of user but I don't know what I should to do to get google calendar events of user after it. 这段代码可以工作并保存用户的访问令牌,但是我不知道该怎么做才能获取用户的Google日历事件。 I had some problem before this and asked it in this question and tried to solve it and now I am here. 在此之前,我遇到了一些问题,并在此问题中提出并试图解决,现在我在这里。 can some one help me? 有人能帮我吗?

in url: 在网址中:

url(r'^new_event/$', views.new_event, name="new_event"),
url(r'^oauth2_callback', OAuth2CallBack.as_view(), name='oauth2_callback'),
url(r'^access_to_google_calendar$', views.access_to_google_calendar, 
                         name="access_to_google_calendar"),

in view: 鉴于:

def access_to_google_calendar(request):
    # Following line is for getting google calendar events of user to show him
    flow = OAuth2WebServerFlow(settings.CLIENT_ID_CALENDAR, 
                             settings.CLIENT_SECRET_CALENDAR,
                             scope='https://www.googleapis.com/auth/calendar',
                               redirect_uri=settings.REDIRECT_URI_CALENDAR)
    generated_url = flow.step1_get_authorize_url()
    return HttpResponseRedirect(generated_url)



class OAuth2CallBack(View):
    def get(self, request, *args, **kwargs):
        code = request.GET.get('code', False)
        if not code:
            return JsonResponse({'status': 'error, no access key received from Google or User declined permission!'})

        flow = OAuth2WebServerFlow(settings.CLIENT_ID_CALENDAR, settings.CLIENT_SECRET_CALENDAR,
                                   scope='https://www.googleapis.com/auth/calendar',
                                   redirect_uri=settings.REDIRECT_URI_CALENDAR)
        credentials = flow.step2_exchange(code)

        http = httplib2.Http()
        http = credentials.authorize(http)

        credentials_js = json.loads(credentials.to_json())
        access_token = credentials_js['access_token']
        # Store the access token in case we need it again!
        username = request.user.username
        with open('token.csv', 'w') as file:
            fieldnames = ['user', 'token']
            writer = csv.DictWriter(file, fieldnames=fieldnames)
            writer.writeheader()
            writer.writerow({'user': username, 'token': access_token})

        request.session['access_token'] = access_token
        return redirect('new_event')

    def post(self, request, *args, **kwargs):
        return HttpResponseNotAllowed('Only GET requests!')



def new_event(request):
        # Here how to get google event of user?????

If you check the Documentation for Google calendar there is sample code for most of the methods. 如果您查看Google日历的文档,则大多数方法都有示例代码。 events.list events.list

page_token = None
while True:
  events = service.events().list(calendarId='primary', pageToken=page_token).execute()
  for event in events['items']:
    print event['summary']
  page_token = events.get('nextPageToken')
  if not page_token:
    break

I finally find out how to create service to get google calendar event just having access token : 我终于找到了如何创建服务来获取具有访问令牌的 Google日历事件:

At first you should get token that is saved before(from csv file or data base or... that here is saved in csv file) 首先,您应该获取之前保存的令牌(从csv文件或数据库或...在此保存在csv文件中)

token = get_from_any_where_you_saved_it_before

and then: (first line is important in this case) 然后:(在这种情况下,第一行很重要)

credentials = client.AccessTokenCredentials(token, 'USER_AGENT')
service = build('calendar', 'v3', credentials=credentials)
google_calendar_events = service.events().list(calendarId='primary', singleEvents=True,
                                          orderBy='startTime').execute()
google_calendar_events = google_calendar_events.get('items', [])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM