简体   繁体   English

如何使用 SlidesSnippet 工具可能用于 google slides api python

[英]How to use SlidesSnippet tool probably for google slides api python

So I am trying to automate creating google slides using SlidesSnippets所以我正在尝试使用 SlidesSnippets 自动创建谷歌幻灯片

( https://github.com/gsuitedevs/python-samples/blob/master/slides/snippets/slides_snippets.py ) 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我拥有所有 api 凭据句柄并可以访问我的 api 服务,但很难理解如何将代码与我的幻灯片目录完全链接

( https://docs.google.com/presentation/u/0/?tgif=d ) ( 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:我把github中的代码重新写了下, init部分如下所示:

 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我得到这个看起来像幻灯片 id 标签的响应,然后当我去

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) ( 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?我在 SlidesSnippets init函数中搞砸了什么吗?

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 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM