简体   繁体   中英

Manually authenticate my user account with Google Calendar API v3

What I'm trying to do:

Allow users that are authenticated and authorized on my site to use a form that allows them to create events on my calendar using Google Calendar's API.

I was able to accomplish this through Google Calendar API v2 by manually authenticating my Google account by doing this:

calendar_service = gdata.calendar.service.CalendarService()
calendar_service.email = 'email@site.com'
calendar_service.password = 'password'
calendar_service.source = 'Calendar'
calendar_service.ProgrammaticLogin()

I could then pipe in the POST date from my site's form and create an event using the API, but I'm at a loss for how to do this with V3 of the API.

I have read the documentation and tried implementing:

created_event = service.events().insert(calendarId='primary', body=event).execute()

But this requires that a service object be created, which in turn requires account authentication through OAuth 2.0, which is not what I'm trying to accomplish. I need to manually authenticate my own calendar instead of asking a user if my application can access their calendar.

I'm sure I'm missing some fundamental logic or making some false assumptions. Any help in the right direction would be appreciated.

You have to use OAuth to authorize your code to access your own calendar ; once you do that, you'll get a token that you can keep. When your code runs again, it should reuse the token to connect to your calendar. Users won't be challenged, but the token might be refreshed from time to time.

Can't suggest any code because I personally hate OAuth with a passion :(

@Ben - The service object can be created either with a key/secret combination OR using a Service Account where you use a email/cert combination and the prn parameter to identify the user on which you want to operate. Both methods require registration in your API console.

Using a Service Account, you could just specify your email address in the prn parameter to insert the event into your calendar. The insert statement stays the same since you're operating on your account with this specification.

credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL, key,
  scope='https://www.googleapis.com/auth/calendar', prn='you@domain.com')
...

created_event = service.events().insert(calendarId='primary', body=event).execute()

Here's an example of that: https://developers.google.com/drive/delegation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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