使用Oauth和Python的Google表单

z31licg0  于 2023-05-27  发布在  Python
关注(0)|答案(1)|浏览(129)

我正在尝试使用Python和OAuth创建一个Google Forms表单。我已经获得了相关作用域的访问令牌。我能够使用访问令牌调用一些受保护的资源。
当我尝试创建一个表单时,我收到一些错误。发现客户端无法识别表单。但它可以识别其他API,例如驱动器。

import os.path

        from google.auth.transport.requests import Request
        from google.oauth2.credentials import Credentials
        from google_auth_oauthlib.flow import InstalledAppFlow
        from googleapiclient.discovery import build
        from googleapiclient.errors import HttpError

        SCOPES = ['https://www.googleapis.com/auth/forms.body']

        creds = Credentials(token="...", client_id="...", client_secret="...")
        service = build('forms', 'v1', credentials=creds)
In [61]: service = build('forms', 'v3', credentials=creds)
---------------------------------------------------------------------------
UnknownApiNameOrVersion                   Traceback (most recent call last)
<ipython-input-61-bcb27d6a5c49> in <module>
----> 1 service = build('forms', 'v3', credentials=creds)

~/gbms_env/lib/python3.9/site-packages/googleapiclient/_helpers.py in positional_wrapper(*args, **kwargs)
    132                 elif positional_parameters_enforcement == POSITIONAL_WARNING:
    133                     logger.warning(message)
--> 134             return wrapped(*args, **kwargs)
    135 
    136         return positional_wrapper

~/gbms_env/lib/python3.9/site-packages/googleapiclient/discovery.py in build(serviceName, version, http, discoveryServiceUrl, developerKey, model, requestBuilder, credentials, cache_discovery, cache, client_options, adc_cert_path, adc_key_path, num_retries, static_discovery)
    289 
    290         try:
--> 291             content = _retrieve_discovery_doc(
    292                 requested_url,
    293                 discovery_http,

~/gbms_env/lib/python3.9/site-packages/googleapiclient/discovery.py in _retrieve_discovery_doc(url, http, cache_discovery, serviceName, version, cache, developerKey, num_retries, static_discovery)
    403             return content
    404         else:
--> 405             raise UnknownApiNameOrVersion("name: %s  version: %s" % (serviceName, version))
    406 
    407     actual_url = url

UnknownApiNameOrVersion: name: forms  version: v3

https://developers.google.com/forms/api/quickstart/python这里的示例可以工作,但它使用了已弃用的库oauth2client
表单API的版本,如v2,v3:

service = build('forms', 'v2', credentials=creds)

我希望你能举个例子。

ut6juiuv

ut6juiuv1#

只需要禁用静态发现。

service = build('forms', 'v1', credentials=creds, static_discovery=False)

相关问题