简体   繁体   中英

How to use SlidesSnippet tool probably for google slides api python

So I am trying to automate creating google slides using SlidesSnippets

( https://github.com/gsuitedevs/python-samples/blob/master/slides/snippets/slides_snippets.py )

I have all api credentials handles and access into my api service but having a hard time understanding how to fully link the code with my slides directory

( https://docs.google.com/presentation/u/0/?tgif=d )

I have taken the code in the github and re-wrote the init section shown below:

 class SlidesSnippets(object): # def __init__(self, service, drive_service, sheets_service, credentials): def __init__(self): # self.credentials = credentials self.credentials = GoogleCredentials.get_application_default() scope = [ 'https://www.googleapis.com/auth/drive', ] self.credentials_scoped = self.credentials.create_scoped(scope) http = self.credentials_scoped.authorize(httplib2.Http()) # self.service = service self.service = build('slides', 'v1', http=http) # self.drive_service = drive_service self.drive_service = build('drive', 'v3', http=http) # self.sheets_service = sheets_service

The comments are what was originally in the class function and then I replaced it with my details.

So when I run this code:

 import slides_snippets as slides slides_init = slides.SlidesSnippets() slides_dict = slides_init.create_presentation("TEST")

I get this response that looks like a slides id tag and then when I go to

and when I try and go to that directory with the tag in it

( https://docs.google.com/presentation/d/ OUTPUT_SLIDE_ID_FROM_create_presentation /edit)

It asks for me to request control and the powerpoint is nowhere to be seen in my slides drive.

Did I mess anything up in my SlidesSnippets init function?

I used some function to init it. Maybe it can help you.

class SlidesSnippets(object): def init (self): self.drive_credentials = None self.slides_credentials = None

def drive_service(self,gdrive_SCOPES = ['https://www.googleapis.com/auth/drive.file']):
    '''gdriver token'''
    if os.path.exists('drivetoken.pickle'):
        with open('drivetoken.pickle', 'rb') as token:
            gdrive_creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not gdrive_creds or not gdrive_creds.valid:
        if gdrive_creds and gdrive_creds.expired and gdrive_creds.refresh_token:
            gdrive_creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', gdrive_SCOPES)
            gdrive_creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('drivetoken.pickle', 'wb') as token:
            pickle.dump(gdrive_creds, token)

    drive_service = build('drive', 'v3', credentials=gdrive_creds)
    self.drive_service,self.drive_credentials = drive_service, gdrive_creds
    return self.drive_service,self.drive_credentials

def slides_service(self,slides_SCOPES = ['https://www.googleapis.com/auth/presentations']):
    '''slides token'''
    if os.path.exists('slidetoken.pickle'):
        with open('slidetoken.pickle', 'rb') as token:
            slides_creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not slides_creds or not slides_creds.valid:
        if slides_creds and slides_creds.expired and slides_creds.refresh_token:
            slides_creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', slides_SCOPES)
            slides_creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('slidetoken.pickle', 'wb') as token:
            pickle.dump(slides_creds, token)

    slides_service = build('slides', 'v1', credentials=slides_creds)
    self.slides_service,self.slides_credentials = slides_service, slides_creds
    return self.slides_service,self.slides_credentials

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