2017-12-17 16 views
1

Python/App EngineでOAuth2ログインを設定しようとしていて、突然動作を停止するまで、ログには、次のことを示している。"google.authの資格が指定されていますが、httplib2をインストールする必要があります。" - 既にインストール済み

'Credentials from google.auth specified, but ' 
ValueError: Credentials from google.auth specified, but google-api-python-client is unable to use these credentials unless google-auth-httplib2 is installed. Please install google-auth-httplib2. 

私は私のプロジェクトのlibフォルダにhttplib2を持っています。私もpip install google-auth-httplib2でインストールしようとしましたが、エラーはまだあります。

import logging 
import jinja2 
import os 
import webapp2 

import httplib2 

from apiclient.discovery import build 
from oauth2client.contrib.appengine import OAuth2Decorator 


from google.appengine.api import users 


decorator = OAuth2Decorator(
    client_id='REMOVED', 
    client_secret='REMOVED', 
    scope='https://www.googleapis.com/auth/plus.login') 


service = build('plus', 'v1') 


class MainHandler(webapp2.RequestHandler): 

    @decorator.oauth_aware 
    def get(self): 

     if decorator.has_credentials(): 
      response = service.people().get(userId="me").execute(http=decorator.http()) 
      # Write the profile data 
      self.response.write(unicode(response)) 
     else: 
      url = decorator.authorize_url() 
      self.response.write('You must login : <a href="'+url+'">Go</a>') 


application = webapp2.WSGIApplication(
    [ 
    ('/', MainHandler), 

    (decorator.callback_path, decorator.callback_handler()) 

    ], 
    debug=True) #remove debug=true before final 
+0

私はちょうどhttplib2を持つことは十分ではないと思い、こちらをご覧は、HTTPSています/ /pypi.python.org/pypi/google-auth-httplib2 –

+0

同様にインポートしてみましょうか? 'import google_auth_httplib2' –

答えて

0

私はrequirements.txtを設定ミスにより、あなたのケースを再現することができ、[1]:

は、ここに私のログインコードです。このサンプル[2]から大きなクエリについてテストを始め、あなたの範囲に変更しました。 ClientIdとClientSecretはjsonファイルにあります。あなたは[3]からそのjsonの内容を得ることができます。 OAuth Credentialを作成し、jsonコンテンツをclient_secret.jsonにコピーする必要があります。

このサンプルでは、​​appengineアプリケーションの依存関係をリストしたrequirements.txtが表示されています。サンプルは、同じパスでローカルlibフォルダ上のすべての依存関係をインストールするために住んでいるフォルダ内に、次のコマンドを実行します。

: ピップは、インストール-r -t requirements.txtのlib/

私requirements.txtは次のようになります

google-api-python-client==1.6.4 
google-auth==1.2.0 
google-auth-httplib2==0.0.2 

次に、そのlib /フォルダでプロジェクトの依存関係を参照できます。 main.pyの習慣があるプログラムのルートには、libフォルダをGAEアプリケーションにロードするappengine_config.pyという名前のファイルがあり、それらのライブラリをPythonプログラムにインポートすることができます。 appengine_config.pyは、次のようになります。また

from google.appengine.ext import vendor 
# Add any libraries installed in the "lib" folder. 
vendor.add('lib') 

、あなたのclientIdとあなたのclientSecretについての内容を配置する必要があるたclient_secrets.jsonは、そこにあります。

bigqueryサンプルと同じインポートでアプリケーションをデプロイすると、動作させる必要があります。

gcloud app deploy 

私はここで変更されたコードを共有しています。それがお役に立てば幸い:

import json 
import os 

import googleapiclient.discovery 
from oauth2client.contrib.appengine import 
OAuth2DecoratorFromClientSecrets 
import webapp2 


# The project id whose datasets you'd like to list 
PROJECTID = 'Your-project-id' 

# Create the method decorator for oauth. 
decorator = OAuth2DecoratorFromClientSecrets(os.path.join(os.path.dirname(__file__), 'client_secrets.json'),scope='https://www.googleapis.com/auth/bigquery') 
#decorator = OAuth2DecoratorFromClientSecrets(os.path.join(os.path.dirname(__file__), 'client_secrets.json'),scope='https://www.googleapis.com/auth/plus.login') 

# Create the bigquery api client 
service = googleapiclient.discovery.build('bigquery', 'v2') 


class MainPage(webapp2.RequestHandler): 

    # oauth_required ensures that the user goes through the OAuth2 
    # authorization flow before reaching this handler. 
    @decorator.oauth_required 
    def get(self): 
     # This is an httplib2.Http instance that is signed with the user's 
     # credentials. This allows you to access the BigQuery API on behalf 
     # of the user. 

     http = decorator.http() 
     response = service.datasets().list(projectId=PROJECTID).execute(http) 

     self.response.out.write('<h3>Datasets.list raw response:</h3>') 
     self.response.out.write('<pre>%s</pre>' % json.dumps(response, sort_keys=True, indent=4,separators=(',', ': '))) 


class MainAware(webapp2.RequestHandler): 
    # credentials. This allows you to access the BigQuery API on behalf 
    # oauth_required ensures that the user goes through the OAuth2 
    # authorization flow before reaching this handler. 
    @decorator.oauth_aware 
    def get(self): 
     # This is an httplib2.Http instance that is signed with the user's 
     # credentials. This allows you to access the BigQuery API on behalf 
     # of the user. 
     if decorator.has_credentials(): 
      http = decorator.http() 
      response = service.datasets().list(projectId=PROJECTID).execute(http) 
      self.response.out.write('<h3>Datasets.list raw response:</h3>') 
      self.response.out.write('<pre>%s</pre>' % json.dumps(response, sort_keys=True, indent=4,separators=(',', ': '))) 

     else: 
      url = decorator.authorize_url() 
      # Write a page explaining why authorization is needed, 
      # and provide the user with a link to the url to proceed. 
      # When the user authorizes, they get redirected back to this path, 
      # and has_credentials() returns True. 
      self.response.out.write(url) 

app = webapp2.WSGIApplication([ 
    ('/', MainPage), 
    ('/aware', MainAware), 
    # Create the endpoint to receive oauth flow callbacks 
    (decorator.callback_path, decorator.callback_handler()) 
], debug=True) 
# [END all] 

[1] https://cloud.google.com/appengine/docs/standard/python/getting-started/python-standard-env#setting_up_libraries_to_enable_development

[2] https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine/standard/bigquery

[3] https://console.cloud.google.com/apis/credentials?project=your-project-id

関連する問題