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