Menu

Erreur de syntaxe Python

Messages postés
1
Date d'inscription
mercredi 3 avril 2019
Statut
Membre
Dernière intervention
3 avril 2019
-
Bonjour,

Je suis nouveau en programmation[/code] Python et je rencontre le problème suivant :

Traceback (most recent call last):
File "/home/bernard/quickstart/quickinsert.py", line 75, in <module>
event = service.events().insert(calendarId='primary', body=event).execute()
NameError: name 'service' is not defined



Avec mes remerciements pour votre aide

Mon code :

from __future__ import print_function
import datetime
import pickle
import os.path
import sys
reload(sys)
sys.setdefaultencoding('utf8')
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
# SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
SCOPES = ['https://www.googleapis.com/auth/calendar']

def main():

    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server()
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('calendar', 'v3', credentials=creds)

    # Call the Calendar API
    now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
    events_result = service.events().list(calendarId='primary', timeMin=now,
                                        maxResults=200, singleEvents=True,
                                        orderBy='startTime').execute()
    events = events_result.get('items', [])

    # fichier=open("/home/bernard/quickstart/bernardinsert.txt","r")
event = {
  'summary': 'Google I/O 2015',
  'location': '800 Howard St., San Francisco, CA 94103',
  'description': 'A chance to hear more about Google\'s developer products.',
  'start': {
    'dateTime': '2015-05-28T09:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
  },
  'end': {
    'dateTime': '2015-05-28T17:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
  },
  'recurrence': [
    'RRULE:FREQ=DAILY;COUNT=2'
  ],
  'attendees': [
    {'email': 'lpage@example.com'},
    {'email': 'sbrin@example.com'},
  ],
  'reminders': {
    'useDefault': False,
    'overrides': [
      {'method': 'email', 'minutes': 24 * 60},
      {'method': 'popup', 'minutes': 10},
    ],
  },
}

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


if __name__ == '__main__':
    main()


Configuration: Linux / Firefox 66.0
Afficher la suite 

Votre réponse

1 réponse

0
Merci
service
est déclaré dans la fonction main et n'existe pas ailleurs, pas difficile de s'en rendre compte.
Commenter la réponse de hippy