使用Python在Google应用程序引擎中使用Google Provisioning API和oAuth2
好的,我想做的是在后台使用Google的资源调配API向Google组添加成员,因为他们在Google course builder(由Google app engine提供支持)中注册/注册 我尝试的第一种方法是使用clientLogin方法,如下所示: 虽然这在AppEngine之外工作,但在实现它之后,我遇到了一些错误并被告知(http://stackoverflow.com/questions/13867535/python-import-gdata-google-apps-engine-course-builder)在应用程序引擎中使用它是一个坏主意,因为它已被弃用 我现在正试图使用这里的python资源调配oAuth示例来实现这一点(https://developers.google.com/google-apps/help/libraries-samples#provisioningv2) 资料来源如下:使用Python在Google应用程序引擎中使用Google Provisioning API和oAuth2,python,google-app-engine,oauth,google-api,google-provisioning-api,Python,Google App Engine,Oauth,Google Api,Google Provisioning Api,好的,我想做的是在后台使用Google的资源调配API向Google组添加成员,因为他们在Google course builder(由Google app engine提供支持)中注册/注册 我尝试的第一种方法是使用clientLogin方法,如下所示: 虽然这在AppEngine之外工作,但在实现它之后,我遇到了一些错误并被告知(http://stackoverflow.com/questions/13867535/python-import-gdata-google-apps-engine
"""Sample for the Provisioning API and the Email Settings API with OAuth 2.0."""
__author__ = 'Shraddha Gupta <shraddhag@google.com>'
from optparse import OptionParser
import gdata.apps
import gdata.apps.emailsettings.client
import gdata.apps.groups.client
import gdata.client
import gdata.gauth
import httplib
API_VERSION = '2.0'
BASE_URL = '/a/feeds/group/%s' % API_VERSION
SCOPE = ('https://apps-apis.google.com/a/feeds/groups/')
HOST = 'apps-apis.google.com'
class OAuth2ClientSample(object):
"""OAuth2ClientSample object demos the use of OAuth2Token for retrieving
Members of a Group and updating Email Settings for them."""
def __init__(self, domain, client_id, client_secret):
"""
Args:
domain: string Domain name (e.g. domain.com)
client_id: string Client_id of domain admin account.
client_secret: string Client_secret of domain admin account.
"""
try:
self.token = gdata.gauth.OAuth2Token(client_id=client_id,
client_secret=client_secret,
scope=SCOPE,
user_agent='oauth2-provisioningv2')
self.uri = self.token.generate_authorize_url()
#print 'Please visit this URL to authorize the application:'
#print self.uri
# Get the verification code from the standard input.
#code = raw_input('What is the verification code? ').strip()
conn = httplib.HTTPConnection(self.uri)
conn.request("GET")
r1 = conn.getresponse()
print r1.read()
self.token.get_access_token(code)
except gdata.gauth.OAuth2AccessTokenError, e:
print 'Invalid Access token, Check your credentials %s' % e
exit(0)
self.domain = domain
self.baseuri = '%s/%s' % (BASE_URL, domain)
self.client = gdata.apps.groups.client.GroupsProvisioningClient(
domain=self.domain, auth_token=self.token)
# Authorize the client.
# This will add the Authorization header to all future requests.
self.token.authorize(self.client)
self.email_client = gdata.apps.emailsettings.client.EmailSettingsClient(
domain=self.domain, auth_token=self.token)
self.token.authorize(self.email_client)
def create_filter(self, feed):
"""Creates a mail filter that marks as read all messages not containing
Domain name as one of their words for each member of the group.
Args:
feed: GroupMemberFeed members whose emailsettings need to updated
"""
for entry in feed.entry:
user_name, domain = entry.member_id.split('@', 1)
if entry.member_type == 'User' and domain == self.domain:
print 'creating filter for %s' % entry.member_id
self.email_client.CreateFilter(user_name,
does_not_have_the_word=self.domain,
mark_as_read=True)
elif entry.member_type == 'User':
print 'User belongs to other Domain %s' %entry.member_id
else:
print 'Member is a group %s' %entry.member_id
def run(self, group):
feed = self.client.RetrieveAllMembers(group)
self.create_filter(feed)
def main():
sample = OAuth2ClientSample('mydomain.mygbiz.com',
'mydomain', 'My client secret')
sample.run('test')
if __name__ == '__main__':
main()
我注释掉了原始代码,它接受令牌的原始输入。这需要在后台无缝运行,因此我需要它自动检索令牌。我已经启动了httplib,但它返回了一个URL错误
我有几个问题
首先,这是我需要做的最简单的方法吗,这似乎有些过头了。clientLogin更优雅、更简单
第二个,如果我必须这样做,那么一旦我有了验证URL,如何准确地检索令牌?我是否使用httplib
我正在认真考虑在Python中模拟一个浏览器。。。为什么这个过程如此复杂
非常感谢您提供的任何帮助。
编辑:我只是想指出,做所有这些的原因首先是为了开发一个课程内建设者,该建设者可能拥有数万到数十万的成员。讨论平台将是google groups,但我们不能让它向公众开放,但我们也不能手动批准每个成员。您可以继续使用ClientLogin,弃用是一个缓慢的过程(https://developers.google.com/accounts/terms). 我的应用程序使用Oauth 2,但我不使用应用程序引擎。您可以从项目的API控制台页面下载client_secrets.json文件(https://code.google.com/apis/console)并从文件中检索机密等。 例如:
import oauth2client.client
import oauth2client.file
import oauth2client.tools
...
oauth2_flow = oauth2client.client.flow_from_clientsecrets('client_secrets.json',
scope='list of scopes',message='Missing client_secrets.json file message')
storage = oauth2client.file.Storage('creedentials.ouath2')
oauth2_credentials = storage.get()
if oauth2_credentials is None or oauth2_credentials.invalid:
oauth2_credentials = oauth2client.tools.run(oauth2_flow, storage)
...
有人有什么建议吗?CTy,非常感谢你的意见。当我在Google App Engine中使用ClientLogin时,它返回了一些错误,当我在我的另一个线程()中报告错误时,我被告知无法使用它,因为它已被弃用。此外,我还找到了一些oAuth2的示例,并使它们正常工作,问题是,我需要以某种方式获得凭证,以便在GAE环境中使用GData库。除此之外,谷歌对所有内容都有示例代码。我开始了一个新的帖子,更详细地介绍了我现在的处境:再次感谢你的帮助。最后我再次尝试了clientLogin,它成功了。我不知道为什么第一次不起作用,因为我使用了完全相同的代码,但是很好。谢谢
#print 'Please visit this URL to authorize the application:'
#print self.uri
# Get the verification code from the standard input.
#code = raw_input('What is the verification code? ').strip()
conn = httplib.HTTPConnection(self.uri)
conn.request("GET")
r1 = conn.getresponse()
print r1.read()
self.token.get_access_token(code)
import oauth2client.client
import oauth2client.file
import oauth2client.tools
...
oauth2_flow = oauth2client.client.flow_from_clientsecrets('client_secrets.json',
scope='list of scopes',message='Missing client_secrets.json file message')
storage = oauth2client.file.Storage('creedentials.ouath2')
oauth2_credentials = storage.get()
if oauth2_credentials is None or oauth2_credentials.invalid:
oauth2_credentials = oauth2client.tools.run(oauth2_flow, storage)
...