[英]QML Calendar and Google Calendar API in Python events integration
[英]Google calendar API integration with Python - Change sender's mail address in Calendar API
我想弄清楚 Google API 與 Python(Django) 的集成以向用戶發送日歷事件。 雖然一切正常,但電子郵件是從我的谷歌服務帳戶郵件地址發送的,其格式如下:
<project_name>-django@<project_name>.iam.gserviceaccount.com
這是來自我的 json 文件的client_email
屬性。 請閱讀此答案以進行實施: https : //stackoverflow.com/a/37933674/3520404
現在,我希望所有郵件都從特定的郵件地址發送,以便人們可以回復郵件。
這是我的代碼:
import httplib2
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
def get_google_calendar_service():
credentials = ServiceAccountCredentials.from_json_keyfile_name(
filename=settings.GOOGLE_SERVICE_ACCOUNT_KEY_FILE_PATH,
scopes=['https://www.googleapis.com/auth/calendar']
)
http = credentials.authorize(httplib2.Http())
service = build('calendar', 'v3', http=http)
return service
def create_event():
service = get_google_calendar_service()
start_datetime = datetime.datetime.now(tz=pytz.utc)
event = service.events().insert(calendarId='<MY_EMAIL_ID>', body={
'summary': 'Foo',
'description': 'Bar',
'start': {'dateTime': start_datetime.isoformat()},
'end': {'dateTime': (start_datetime + timedelta(minutes=15)).isoformat()},
}).execute()
print(event)
我想從<MY_EMAIL_ID>
發送所有日歷邀請郵件,但這沒有發生。 無論如何在發送日歷邀請郵件時控制FROM EMAIL ADDRESS
? 請幫忙。
這是因為 serviceaccount 是創建事件的帳戶。 不過,您確實有多種選擇。
1) 如果您有 G Suite 帳戶,請在服務帳戶上使用全域委托,並模擬您要使用的<MY_EMAIL_ID>
。 當然,它必須在 G Suite 域中。 此示例適用於 Admin SDK,但它仍然是模擬的最佳示例。 行credentials = credentials.create_delegated(user_email)
是我們感興趣的。
2) 沒有 G Suite 帳號,那么你需要使用 OAuth2 登錄流程來獲取<MY_EMAIL_ID>
OAuth2 憑據,然后使用這些憑據對請求進行身份驗證。 提供了大量文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.