我真的需要一些幫助,我已經在這個近2周。谷歌應用程序引擎和組配置
我想要做的是在GAE(Google App Engine)內部使用Google的配置API,使用oAuth2。我知道有一些使用oAuth1來實現這個功能的例子。但我的理解是,oAuth1現在已被棄用,我們必須使用oAuth2,如果我錯了,請糾正我。
我已經沖刷互聯網,我能找到工作從唯一的例子是這樣的:
http://gdata-samples.googlecode.com/svn-history/r232/trunk/gdata/youtube-oauth2-app-engine/main.py
其他的例子我發現使用OAuth 1,或者他們沒有設計使用與App Engine一起使用。
我從上面的例子中所採取的代碼,並試圖修改它與網上論壇配置API的工作,這裏是我的代碼:
import os
from gdata.alt import appengine
from gdata.service import RequestError
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util
from oauth2client.appengine import OAuth2Decorator
import gdata.auth
import gdata.apps.groups.client
import gdata.client
import httplib2
API_VERSION = '2.0'
BASE_URL = '/a/feeds/group/%s' % API_VERSION
# HACK to use the Python GData client library with OAuth 2 tokens.
# We use the methods that deal with AuthSub tokens.
gdata.auth.AUTHSUB_AUTH_LABEL = "OAuth "
class MainHandler(webapp.RequestHandler):
# The client_id and client_secret are copied from the API Access tab on
# the Google APIs Console <http://code.google.com/apis/console>
oauth2_decorator = OAuth2Decorator(
client_id="myClientID.apps.googleusercontent.com",
client_secret="myClientSecret",
scope="https://apps-apis.google.com/a/feeds/groups/")
# This decorator ensures that whenever this page is served, we have a valid
# OAuth 2 token for the user.
@oauth2_decorator.oauth_required
def handle_exception(self, exception, debug_mode):
"""Handle OAuth 2 token expirations by forcing a refresh.
For newer Google APIs, refreshes are handled automatically by the client
library, but for GData APIs, we need to explicitly force this behavior.
"""
if (isinstance(exception, RequestError) and
exception.args[0]["status"] == 401):
body = exception.args[0]["body"]
if "Token invalid - Invalid AuthSub token." in body:
self.oauth2_decorator.credentials._refresh(httplib2.Http().request)
self.redirect(self.request.url)
return
webapp.RequestHandler.handle_exception(self, exception, debug_mode)
# This decorator ensures that whenever this page is served, we have a valid
# OAuth 2 token for the user.
@oauth2_decorator.oauth_required
def get(self):
self.domain='testdomain123456.mygbiz.com'
self.baseuri = '%s/%s' % (BASE_URL, 'testdomain123456.mygbiz.com')
self.token = self.oauth2_decorator.credentials.access_token
self.client = gdata.apps.groups.client.GroupsProvisioningClient(
domain=self.domain, auth_token=self.token)
self.client.SetAuthSubToken(self.token)
params = dict(
logout_url=users.create_logout_url(self.request.uri),
memberFeed = self.client.RetrieveAllMembers('test')
)
path = os.path.join(os.path.dirname(__file__), 'templates', 'index.html')
self.response.out.write(template.render(path, params))
def main():
application = webapp.WSGIApplication([('/', MainHandler)], debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
我部署這http://appplat4.appspot.com,並且你可以看到它返回一個500服務器錯誤。我有與app.yaml
相同的目錄中的所有必要的庫,我有我的谷歌API設置爲域。
截圖:
我一直在努力盡我所能來配置組從內部GAE沒有成功。請幫助,如果可以,任何輸入量讚賞。
好吧,謝謝CTY,我將與這一點,看看我能來浪費時間與...一起。非常感謝。 – Russell
我得到了這個美麗的工作。唯一的問題是將它與GAE結合,但看起來我並沒有獲得更多的反饋意見,所以我會接受你的答案。再次感謝。 – Russell