Python Youtube API 客户端警告

Python Youtube API Client Warnings

TLDR, using google-api-python-client, it's giving me a couple of warnings,

WARNING:googleapiclient.discovery_cache:file_cache is unavailable when using oauth2client >= 4.0.0
Traceback (most recent call last):
  File "/home/user/.virtualenvs/tmp/lib/python3.6/site-packages/googleapiclient/discovery_cache/__init__.py", line 36, in autodetect
    from google.appengine.api import memcache
ModuleNotFoundError: No module named 'google'

And I want to hide them. How do I hide them?


详情

我正在使用 google-api-python-client==1.6.2 使用 YouTube data API 执行搜索。在这种情况下我不需要 OAuth,所以除了 google-api-python-client.

我没有安装任何东西

当我 运行 我的代码时,我收到一条带有几个回溯的长警告。我的应用程序仍在 运行ning,因为我仍然可以使用 curl 访问服务器并得到结果。

WARNING:googleapiclient.discovery_cache:file_cache is unavailable when using oauth2client >= 4.0.0
Traceback (most recent call last):
  File "/home/user/.virtualenvs/tmp/lib/python3.6/site-packages/googleapiclient/discovery_cache/__init__.py", line 36, in autodetect
    from google.appengine.api import memcache
ModuleNotFoundError: No module named 'google'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.virtualenvs/tmp/lib/python3.6/site-packages/googleapiclient/discovery_cache/file_cache.py", line 33, in <module>
    from oauth2client.contrib.locked_file import LockedFile
ModuleNotFoundError: No module named 'oauth2client.contrib.locked_file'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.virtualenvs/tmp/lib/python3.6/site-packages/googleapiclient/discovery_cache/file_cache.py", line 37, in <module>
    from oauth2client.locked_file import LockedFile
ModuleNotFoundError: No module named 'oauth2client.locked_file'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.virtualenvs/tmp/lib/python3.6/site-packages/googleapiclient/discovery_cache/__init__.py", line 41, in autodetect
    from . import file_cache
  File "/home/user/.virtualenvs/tmp/lib/python3.6/site-packages/googleapiclient/discovery_cache/file_cache.py", line 41, in <module>
    'file_cache is unavailable when using oauth2client >= 4.0.0')
ImportError: file_cache is unavailable when using oauth2client >= 4.0.0

这是我的代码,

my_package/main.py

from my_package.server import app

HOST = 'localhost'
PORT = 8100
app.run(HOST, PORT)

my_package/server.py

from flask import Flask, request
from flask_restful import Resource, Api
from logging import info
from my_package.youtube import YouTube


app = Flask(__name__)
api = Api(app)

yt_client = YouTube()


class Search(Resource):
    def get(self, query):
        info("Handling `get` request for the resource 'Search'.")
        return yt_client.search(query)

api.add_resource(Search, '/search/<string:query>')


if __name__ == '__main__':
    info('Application starting.')
    app.run(debug=True)

my_package/youtube.py

from logging import info
import apiclient as google


class YouTube:
    MAX_RESULTS = 25

    def __init__(self):
        info('Creating a YouTube API instance.')
        self.API_KEY = 'MY_API_KEY'
        self.youtube = google.discovery.build('youtube', 'v3',
                                              developerKey=self.API_KEY)

    def search(self, query):
        info(f"Performing a search for '{query}'")
        results = self.youtube.search().list(
            q=query,
            part='snippet',
            maxResults=self.MAX_RESULTS
        ).execute()

        return results.get('items', [])

这是我的 requirements.txt

aniso8601==1.2.1
certifi==2017.4.17
chardet==3.0.3
click==6.7
Flask==0.12.2
Flask-RESTful==0.3.6
google-api-python-client==1.6.2
httplib2==0.10.3
idna==2.5
itsdangerous==0.24
Jinja2==2.9.6
MarkupSafe==1.0
oauth2client==4.1.0
pyasn1==0.2.3
pyasn1-modules==0.0.9
python-dateutil==2.6.0
pytz==2017.2
requests==2.17.3
rsa==3.4.2
six==1.10.0
uritemplate==3.0.0
urllib3==1.21.1
Werkzeug==0.12.2

将构建调用中的 cache_discovery 参数设置为 False。

google.discovery.build('youtube','v3',developerKey=self.API_KEY,
                       cache_discovery=False)

这将阻止导入正在记录警告的 cache.discovery 模块。