【问题标题】:"Credentials from google.auth specified, but httplib2 needs to be installed" - Already installed“指定了来自 google.auth 的凭据,但需要安装 httplib2” - 已安装
【发布时间】:2017-12-20 03:23:48
【问题描述】:

我正在尝试使用 Python/App Engine 设置 OAuth2 登录,并让它短暂工作,直到它突然停止工作。日志显示如下:

'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

【问题讨论】:

标签: python google-app-engine authentication oauth-2.0


【解决方案1】:

我可以通过错误配置 requirements.txt [1] 来重现您的案例。我从这个样本 [2] 开始关于大查询的测试并修改到您的范围。这里 ClientId 和 ClientSecret 在一个 json 文件中。您可以从 [3] 中获取该 json 的内容。您需要创建 OAuth 凭据并将 json 内容复制到您的 client_secret.json。

在示例中,您可以看到一个 requirements.txt,其中列出了 appengine 应用程序的依赖项。在示例所在的文件夹中运行下一个命令,以将所有依赖项安装到同一路径中的本地 lib 文件夹中: pip install -r requirements.txt -t lib/

我的 requirements.txt 看起来像:

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

然后您可以在该 lib/ 文件夹中浏览项目的依赖项。在程序的根目录中,main.py 习惯,有一个名为 appengine_config.py 的文件,它将 lib 文件夹加载到您的 GAE 应用程序中,并使您能够在 Python 程序中导入这些库。 appengine_config.py 看起来像:

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

另外,还有client_secrets.json,你需要在其中放置关于你的clientId和你的clientSecret的内容。

使用与 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 2020-09-15
    • 2016-04-17
    • 2019-01-08
    • 2018-09-11
    相关资源
    最近更新 更多