当通过webapp2重定向时,谷歌云端点不加载



我有一个web应用程序运行在谷歌应用程序引擎。我通过云端点实现了一个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加载:

<head>
  ...   
  <script type="text/javascript">
    function init() {
      var apisToLoad;
      var loadCallback = function() {
        console.log(88);
        if (--apisToLoad == 0) {
          signin(true, userAuthed);
        }
      };
      apisToLoad = 2; // must match number of calls to gapi.client.load()
      apiRoot = '//' + window.location.host + '/_ah/api';
      console.log(apiRoot);
      gapi.client.load('books', 'v1', loadCallback, apiRoot);
      gapi.client.load('oauth2', 'v2', loadCallback);
    }
    function authorizeCallback()
    {
    }
    function signin(mode, authorizeCallback) {
      gapi.auth.authorize({client_id: '480333XXXXXXXXXXXXXXo6lckcrt5sehee3dg.apps.googleusercontent.com',
        scope: 'https://www.googleapis.com/auth/userinfo.email', immediate: true},
        authorizeCallback);
    }
    function userAuthed() {
      var request = gapi.client.oauth2.userinfo.get().execute(function(resp) {
        if (!resp.code) {
          // User is signed in, call my Endpoint
          start_spinner();
          gapi.client.books.queryBooks({"subject":"ita"}).execute(function(q) {
            create_content(q.items, "ita");
            add_searches();
          });
        }
      });
    }
    </script>
     <script src="https://apis.google.com/js/client.js?onload=init"></script>
</head>
<body>
项目/index . html

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

webapp2 main.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)
编辑:

app.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资源管理器中访问,否则无法访问。所以错误在这两行,但我不知道为什么。

假设您确实显示了所有相关代码,错误消息表明gapi.client.booksundefined在这行(唯一引用queryBooks):

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

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

如果确认,检查您的相关代码,最终您的导入和API安装在GAE中。

我不知道为什么它工作,但我解决了它:

我改了这个:

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

:

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

相关内容

最新更新