如何为Google Slides API Python使用SlidesSnippet工具

时间:2019-03-14 16:27:56

标签: python-3.x google-api google-slides-api

所以我正在尝试使用SlidesSnippets自动化创建Google幻灯片

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

我拥有所有api凭据句柄并可以访问我的api服务,但是很难理解如何将代码与幻灯片目录完全链接

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

我已经在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

注释是类函数中的原始注释,然后我用我的详细信息替换了注释。

所以当我运行这段代码时:

import slides_snippets as slides

slides_init = slides.SlidesSnippets()
slides_dict = slides_init.create_presentation("TEST")

我得到的响应看起来像一张幻灯片id标签,然后当我进入

,当我尝试进入带有标签的目录

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

它要求我请求控制,而幻灯片在幻灯片驱动器中无处可见。

我在SlidesSnippets init 函数中弄乱了吗?

1 个答案:

答案 0 :(得分:0)

我使用了一些函数来初始化它。也许可以帮到您。

SlidesSnippets(object)类:     def init (自己):         self.drive_credentials =无         self.slides_credentials =无

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