2017-06-03 4 views
1

TLDR、google-api-python-clientを使用して、それは、私に警告のカップルを与えるPythonのYouTubeのAPI Clientの警告

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' 

そして、私はそれらを非表示にしたいです。どのように私はそれらを隠すのですか?


私はYouTube data APIを使用して検索を実行するためにgoogle-api-python-client==1.6.2を使用しています詳細

。この場合OAuthは必要ないので、google-api-python-client以外のものはインストールしていません。

コードを実行すると、いくつかのTracebackで長い警告が表示されます。私のアプリケーションはまだ実行されているので、私はカールを使用してサーバーにアクセスして結果を得ることができます。

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) 

01、私のコードです

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', []) 

ここでFalseにビルド呼び出しでcache_discoveryパラメータを設定し、私の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 
+0

私は、 'logging.getLogger( 'google.apiclient.discovery_cache')を使ってみました。setLevel(logging.ERROR); googleと同様にimport apiclientを実行しますが、違いはありません – francium

+1

ビルドコールのcache_discoveryパラメータをFalseに設定すると、 google.discovery.build( 'youtube'、 'v3'、developerKey = self.API_KEY、cache_discovery = False)。あなたの警告を記録している試行されたインポートを妨げるようなソースに目を通す。とにかく、使用する適切なキャッシュが見つからない場合にのみ、その警告をスローするため、このモジュールから何のメリットも得られません。 – clockwatcher

+0

@clockwatcher cache_discoveryをFalseに設定すると、警告が消えました。何の利益も得られていないのはどういう意味ですか? youtube APIを取得するために 'discovery.build'を使用しないことを提案していますか?より良い方法ですか?私はGoogleのサンプルコードからAPI呼び出しを得ましたが、そこではOAuthを使用しているようですが、そうではありません。 – francium

答えて

2

です。

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

これは、警告を記録しているcache.discoveryモジュールのインポートを妨げます。