1
TLDR,使用
google-api-python-client
,它給了我一些警告,的Python的Youtube API客戶端警告
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'
我想隱藏起來。我如何隱藏它們?
詳細
我使用google-api-python-client==1.6.2
執行使用YouTube data API
搜索。在這種情況下,我不需要OAuth,所以除了google-api-python-client
之外我沒有安裝任何東西。
當我運行我的代碼時,我得到了一個很長的警告與幾個回溯。我的應用程序仍在運行,因爲我仍然可以使用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
我試過使用 'logging.getLogger('google.apiclient.discovery_cache').setLevel(logging.ERROR); 導入apiclient as google',但沒有區別 – francium
您可以嘗試將您的構建調用中的cache_discovery參數設置爲False。 google.discovery.build('youtube','v3',developerKey = self.API_KEY,cache_discovery = False)。看着看起來會阻止嘗試導入的記錄您的警告的源代碼。無論如何,您無法從該模塊中獲得任何好處,因爲如果找不到適當的緩存以供使用,它只會引發該警告。 – clockwatcher
@clockwatcher將cache_discovery設置爲False確實使警告消失。 「沒有得到任何好處」是什麼意思?你是否建議不使用'discovery.build'來獲取YouTube的API?是更好的方法嗎?我從谷歌示例代碼中獲得了api調用,但他們似乎在那裏使用了OAuth,我沒有。 – francium