“指定了google.auth的凭据,但需要安装httplib2” - 已安装

问题描述 投票:2回答:1

我正在尝试使用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个回答
0
投票

我可以通过错误配置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

© www.soinside.com 2019 - 2024. All rights reserved.