通过webapp2重定向时未加载Google云端点

2024-06-16 10:34:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个在谷歌应用引擎上运行的网络应用程序。我通过云端点实现了一个API,并通过Javascript在这个应用程序中使用它。但是,为了让用户登录,我使用webapp2来处理这个过程。当用户成功登录后,他们将被重定向到主页。在我添加webapp2重定向之前,一切似乎都很正常,但是现在我得到了一个错误:

GET http://localhost:10080/_ah/api/discovery/v1/apis/books/v1/rest?fields=rootUrl%2CservicePath%2Cresources%2Cparameters%2Cmethods&pp=0 500 (OK)

Uncaught TypeError: Cannot read property 'queryBooks' of undefined

我觉得这真的很奇怪,因为我做的更多的只是从一个页面重定向到另一个页面。我试图刷新页面,但错误仍然存在。我试图通过javascript处理登录,但太痛苦了。在

以下是相关代码:

gapi加载:

^{pr2}$

项目/索引.html在

<a href="{{ url|safe }}" class="mdl-button mdl-js-button mdl-button--accent">
  Accedi
</a>

网络应用程序2主.py在

    (all the imports)

    JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
    extensions=['jinja2.ext.autoescape'],
    autoescape=True)


class MainPage(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()

        url = users.create_login_url(self.request.uri + 'login')
        if user:

            user_id = getUserId(user)
            p_key = ndb.Key(Profile, user_id)
            profile = p_key.get()

            if profile:
                self.redirect('/partials/home.html', permanent = True)


        template_values = {'url': url}

        template = JINJA_ENVIRONMENT.get_template('index.html')
        self.response.write(template.render(template_values))


class LogIn(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()

        if user:
            user_id = getUserId(user)
            p_key = ndb.Key(Profile, user_id)
            profile = p_key.get()

            if profile:
                self.redirect('partials/home.html', permanent = True)
            else:
                profile = Profile(
                    key = p_key,
                    nickName = user.nickname(), 
                    firstName = "Test",
                    lastName = "Test",
                    mainEmail = user.email()
                )
                # save the profile to datastore
                profile.put()
                self.redirect('/partials/home.html')

        else:
            self.redirect('/')

app = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/login', LogIn)
], debug=True)

编辑:

这是应用程序yaml代码:

application: project-books
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:

- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: /js
  static_dir: static/js

- url: /img
  static_dir: static/img

- url: /css
  static_dir: static/css

- url: /partials
  static_dir: static/partials

#- url: /.*
#  script: main.app

- url: /_ah/spi/.*
  script: books.api
  secure: always

libraries:

- name: endpoints
  version: latest

- name: pycrypto
  version: latest

- name: webapp2
  version: latest

- name: jinja2
  version: latest

当我评论#- url: /.* # script: main.app

API正在工作,可以在本地主机上的API资源管理器中访问,否则就无法访问。因此,错误出现在这两行代码中,但我无法找出原因。在


Tags: keyselftrueurljinja2getifversion
2条回答

我不知道为什么有用,但我解决了:

我改了这个:

- url: /.*
  script: main.app

- url: /_ah/spi/.*
  script: books.api
  secure: always

为此:

^{pr2}$

假设您确实显示了所有相关代码,则错误消息表明gapi.client.books在这一行是未定义的(对queryBooks的唯一引用):

gapi.client.books.queryBooks({"subject":"ita"}).execute(function(q) {

您可能希望在调试消息中显示它以确认这一点。在

如果确认,请检查您的相关代码,最后检查您的导入和您的API installation in GAE。在

相关问题 更多 >