Last active
August 26, 2022 19:30
-
-
Save FelipeSBarros/c12eca33922ebe43ee87058ac87cb068 to your computer and use it in GitHub Desktop.
code snippet para autorização e remoção de um usuário como visualizador de um Google calendario
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pickle | |
from os import path | |
from google_auth_oauthlib.flow import InstalledAppFlow | |
from decouple import config | |
from google.auth.transport.requests import Request | |
from googleapiclient.discovery import build | |
CLIENT_SECRET = config("GOOGLE_CLIENT_SECRET") | |
GOOGLE_TOKEN = config("GOOGLE_TOKEN") | |
SCOPES = [ | |
"https://www.googleapis.com/auth/calendar", | |
] | |
def get_credentials_google(): | |
# OPEN THE BROWSER TO AUTHORIZE | |
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET, SCOPES) | |
creds = flow.run_local_server(port=0) | |
# WE SAVE THE CREDENTIALS | |
pickle.dump(creds, open(GOOGLE_TOKEN, "wb")) | |
return creds | |
def get_calendar_service(): | |
creds = None | |
if path.exists(GOOGLE_TOKEN): | |
creds = pickle.load(open(GOOGLE_TOKEN, "rb")) | |
# IF IT EXPIRED, WE REFRESH THE CREDENTIALS | |
if not creds or not creds.valid: | |
if creds and creds.expired and creds.refresh_token: | |
creds.refresh(Request()) | |
else: | |
creds = get_credentials_google() | |
service = build("calendar", "v3", credentials=creds) | |
return service | |
service = get_calendar_service() | |
# lista os calendarios disponiveis | |
gcal_list = service.calendarList().list().execute() | |
# Acessa informacoes do calendario | |
# 'Defesa/Ministro/Walter Souza Braga Netto' | |
gcal_list['items'][1]["summary"] | |
gcal_defesa_id = gcal_list['items'][1]["id"] | |
# deifnindo a regra de autorizacao. Neste caso, será dada autorizacao a um usuário (poderia ser grupo de usuários ou domain), a atribuicao (role), neste caso, visualizar, e a id ("email do google) do usuário. | |
rule = { | |
'scope': { | |
'type': 'user', | |
'value': 'felipe.b4rros@gmail.com', | |
}, | |
'role': 'reader' | |
} | |
created_rule = service.acl().insert( | |
calendarId=gcal_defesa_id, | |
body=rule).execute() | |
print(created_rule['id']) | |
# O felipe.b4rros receberá um email dizendo que "fiquem sabendo" quer compartilhar uma agenda. | |
# Ele tem que clicar em adicionar agenda. A agenda será adicionada no calendario em "outras agendas"... | |
# para desautorizar o felikpe.b4rros a visualizar a agenda e necessario passar um ruleID | |
# que nada mais é que o Id do calendario a ser removido e o scope type passado na autorizacao. | |
fb_rule_ID = f'{rule.get("scope").get("type")}:{rule.get("scope").get("value")}' | |
service.acl().delete(calendarId=gcal_defesa_id, ruleId=fb_rule_ID).execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment