Python &引用;指定了来自google.auth的凭据,但需要安装httplib2已安装
我正在尝试使用Python/AppEngine设置OAuth2登录,并让它短暂工作,直到它突然停止工作。日志显示以下内容:Python &引用;指定了来自google.auth的凭据,但需要安装httplib2已安装,python,google-app-engine,authentication,oauth-2.0,Python,Google App Engine,Authentication,Oauth 2.0,我正在尝试使用Python/AppEngine设置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-httpli
'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安装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
导入日志
进口金玉2
导入操作系统
导入webapp2
导入httplib2
从apiclient.discovery导入生成
从oauth2client.contrib.appengine导入OAuth2Decorator
从google.appengine.api导入用户
decorator=OAuth2Decorator(
客户_id='已删除',
客户_secret='已删除',
范围=https://www.googleapis.com/auth/plus.login')
服务=构建('plus','v1')
类MainHandler(webapp2.RequestHandler):
@decorator.oauth_-aware
def get(自我):
如果decorator.has_凭据():
response=service.people().get(userId=“me”).execute(http=decorator.http())
#写入配置文件数据
self.response.write(unicode(响应))
其他:
url=decorator.authorize_url()
self.response.write('您必须登录:')
application=webapp2.WSGIApplication(
[
(“/”,MainHandler),
(decorator.callback\u路径,decorator.callback\u处理程序())
],
调试=真)#在最终测试之前删除调试=真
我可以通过错误配置requirements.txt[1]重现您的案例。我从这个关于大查询的示例[2]开始测试,并修改为您的范围。这里ClientId和ClientSecret位于一个json文件中。您可以从[3]获取该json的内容。您需要创建OAuth凭据,并将json内容复制到您的客户机_secret.json
在示例中,您可以看到requirements.txt,其中列出了appengine应用程序的依赖项。在示例所在的文件夹中运行下一个命令,以在同一路径中安装本地lib文件夹上的所有依赖项:
pip安装-r requirements.txt-t lib/
My requirements.txt看起来像:
google-api-python-client==1.6.4
google-auth==1.2.0
google-auth-httplib2==0.0.2
from google.appengine.ext import vendor
# Add any libraries installed in the "lib" folder.
vendor.add('lib')
然后,您可以浏览该库/文件夹上项目的依赖项。在程序的根目录main.py habits中,有一个名为appengine_config.py的文件,它将lib文件夹加载到GAE应用程序中,使您能够在Python程序上导入这些库。appengine_config.py如下所示:
google-api-python-client==1.6.4
google-auth==1.2.0
google-auth-httplib2==0.0.2
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]
导入json
导入操作系统
导入GoogleAppClient.discovery
从oauth2client.contrib.appengine导入
OAuth2DecoratorFromClientSecrets
导入webapp2
#要列出其数据集的项目id
PROJECTID='您的项目id'
#为oauth创建方法装饰器。
decorator=OAuth2DecoratorFromClientSecrets(os.path.join(os.path.dirname(_文件__)),'client_secrets.json'),scope='https://www.googleapis.com/auth/bigquery')
#decorator=OAuth2DecoratorFromClientSecrets(os.path.join(os.path.dirname(_文件__)),'client_secrets.json'),scope='https://www.googleapis.com/auth/plus.login')
#创建BigQueryAPI客户端
service=googleapiclient.discovery.build('bigquery','v2')
类主页(webapp2.RequestHandler):
#oauth_required确保用户通过OAuth2
#到达此处理程序之前的授权流。
@需要decorator.oauth_
def get(自我):
#这是一个使用用户的
#证书。这允许您代表用户访问BigQuery API
#用户的名称。
http=decorator.http()
response=service.datasets().list(projectd=projectd).execute(http)
self.response.out.write('Datasets.list原始响应:')
self.response.out.write(“%s”%json.dumps(响应,排序键=True,缩进=4,分隔符=(',',':'))
类MainAware(webapp2.RequestHandler):
#证书。这允许您代表用户访问BigQuery API
#oauth_required确保用户通过OAuth2
#到达此处理程序之前的授权流。
@decorator.oauth_-aware
def get(自我):
#这是一个使用用户的
#证书。这允许您代表用户访问BigQuery API
#用户的名称。
如果decorator.has_凭据():
http=decorator.http()
response=service.datasets().list(projectd=projectd).execute(http)
self.response.out.write('Datasets.list原始响应:')
self.response.out.write(“%s”%json.dumps(响应,排序键=True,缩进=4,分隔符=(',',':'))
其他:
url=decorator.authorize_url()
#写一页解释为什么需要授权,
#并向用户提供指向url的链接以继续。
#当用户授权时,他们会被重定向回该路径,
#并且has_credentials()返回True。
self.response.out.write(url)
app=webapp2.WSGIApplication([
(“/”,主页),
('/aware',MainAware),
#创建端点以接收oauth流回调
(decorator.callback\u路径,decorator.callback\u处理程序())
],debug=True)
#[全部结束]
[1]
[2]
[3] 我认为仅仅拥有httplib2是不够的,看看这里,然后尝试导入它<代码>导入google\u auth\u httplib2