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

Google Cloud Endpoints not Loading when redirect via webapp2

我在 Google App Engine 上有一个网络应用 运行。我通过 Cloud Endpoints 实现了一个 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>

project/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.books 在这一行是 undefined(对 queryBooks 的唯一引用):

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

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

如果确认检查你的相关代码,最终你的导入和你的API installation in 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