0

정말 도움이 필요합니다. 거의 2 주간 계속했습니다.Google App Engine 및 그룹 프로비저닝

내가하려는 것은 oAuth2를 사용하여 GAE (Google App Engine) 내부의 Google 프로비저닝 API를 사용하는 것입니다. 나는 이것을 달성하기 위해 oAuth1을 사용하는 예제가 몇 가지 있다는 것을 알고있다. 내 이해 비록 oAuth1 지금은 deprecated이며 우리는 oAuth2를 사용해야합니다, 내가 틀렸다면 나를 수정하십시오. 내가 인터넷과 내가에서 일을 찾을 수있는 유일한 예를 흐르고했습니다

은 이것이다 :

http://gdata-samples.googlecode.com/svn-history/r232/trunk/gdata/youtube-oauth2-app-engine/main.py

다른 예 내가 사용하여 OAuth 하나를 발견했습니다, 또는 그들이 사용하도록 설계되지 않습니다 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과 같은 디렉토리에 필요한 모든 라이브러리를 가지고 있으며 도메인에 대해 내 Google API를 설정했습니다.

스크린 샷 :enter image description here

내가 더 성공 GAE 내에서 제공 그룹을 위해 할 수있는 모든 노력했습니다. 만약 당신이 할 수 있으면, 어떤 입력가 인정됩니다.

답변

2

이 애플 리케이션 엔진을 해결하지 않고는 OAuth를 2와 GDATA 클라이언트를 사용하여 명령 줄 예입니다

import sys 
import os 
import webbrowser 
import gdata.gauth 
import oauth2client.client 
import oauth2client.file 
import oauth2client.tools 
import gdata.gauth 
import gdata.client 
import gdata.apps.groups.client 

APICONSOLE = 'https://code.google.com/apis/console' 
SCOPES = 'https://apps-apis.google.com/a/feeds/groups/' 
OAUTH2FILENAME = 'credentials.oauth2' 
OAUTH2JSONFILE = 'client_secrets.json' 
OAUTH2USERAGENT = 'GROUPS' 
MISSING_OAUTHJSONFILE_MESSAGE = """ 
You must create or download a client secrets json file (%s) 
from the Google APIs console <https://code.google.com/apis/console>. 
Attemping to open page with your browser ... 
""" % os.path.join(os.path.dirname(__file__), OAUTH2JSONFILE) 

# populate with approprate values 
DOMAIN = 'your-domain' 
GROUP_ID = '[email protected]' 

if not os.path.isfile(OAUTH2JSONFILE): 
    message = MISSING_OAUTHJSONFILE_MESSAGE 
    print message 
    try: 
    webbrowser.open(str(APICONSOLE)) 
    except Exception, e: 
    print "Error opening web page" 
    sys.exit(1) 
    message = 'When %s is created/downloaded press Enter to continue ... ' %(OAUTH2JSONFILE) 
    raw_input(message) 
oauth2_flow = oauth2client.client.flow_from_clientsecrets(OAUTH2JSONFILE, 
    scope=SCOPES,message=MISSING_OAUTHJSONFILE_MESSAGE) 
storage = oauth2client.file.Storage(OAUTH2FILENAME) 
oauth2_credentials = storage.get() 
if oauth2_credentials is None or oauth2_credentials.invalid: 
    oauth2_credentials = oauth2client.tools.run(oauth2_flow, storage) 
oauth2_token = gdata.gauth.OAuth2Token(
    client_id=oauth2_credentials.client_id, 
    client_secret=oauth2_credentials.client_secret, 
    scope=SCOPES, 
    user_agent=OAUTH2USERAGENT, 
    access_token=oauth2_credentials.access_token, 
    refresh_token=oauth2_credentials.refresh_token) 
# authorize client 
groups_client = oauth2_token.authorize(
    gdata.apps.groups.client.GroupsProvisioningClient(domain=DOMAIN)) 
print 'Authorized domain %s . . .\n' %(DOMAIN) 
group_entry = groups_client.RetrieveGroup(group_id=GROUP_ID) 
print group_entry.group_id 
print group_entry.group_name 
print group_entry.description 
print group_entry.email_permission 

sys.exit(0) 
+0

좋아요, 감사에게, 나는이와 내가 올 수 있는지와 주위를 혼란거야 CTY을 와 함께. 대단히 감사합니다. – Russell

+0

나는 아름답게 일하고있다. 문제는 GAE와 통합하는 것 뿐이지 만, 더 많은 피드백을 받고있는 것 같지 않으므로 답변을 수락 할 것입니다. 다시 한번 감사드립니다. – Russell