我有一個在Google App Engine上運行的Web應用程序。我通過雲端點實現了一個API,並通過Javascript在此應用中使用它。但是,要記錄用戶,我使用webapp2來處理這個過程。當用戶成功登錄後,他們被重定向到主頁。一切似乎都在我加入webapp2的重定向到正常工作,但現在我得到這個錯誤:當通過webapp2重定向時Google雲端點無法加載
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瀏覽器直接訪問,否則就不能。所以錯誤是在這兩行中,但我無法弄清楚爲什麼。
我剛纔看到的API不在插入webapp2代碼後正確部署到Google。我對此很陌生。我沒有更改API代碼中的任何內容 –