Can we access GMAIL API using Service Account?

前端 未结 6 1692
予麋鹿
予麋鹿 2020-11-29 07:20

I have a desktop application to read mail using GMAIL API over REST Interface. I want to use service account so that we can download the mails using domain setting and user

6条回答
  •  余生分开走
    2020-11-29 07:42

    Here is a little bit of python 3.7:

    from google.oauth2 import service_account
    from googleapiclient.discovery import build
    
    def setup_credentials():
        key_path = 'gmailsignatureproject-zzz.json'
        API_scopes =['https://www.googleapis.com/auth/gmail.settings.basic',
                     'https://www.googleapis.com/auth/gmail.settings.sharing']
        credentials = service_account.Credentials.from_service_account_file(key_path,scopes=API_scopes)
        return credentials
    
    
    def test_setup_credentials():
        credentials = setup_credentials()
        assert credentials
    
    
    def test_fetch_user_info():
        credentials = setup_credentials()
        credentials_delegated = credentials.with_subject("tim@vci.com.au")
        gmail_service = build("gmail","v1",credentials=credentials_delegated)
        addresses = gmail_service.users().settings().sendAs().list(userId='me').execute()
        assert gmail_service
    

提交回复
热议问题