1

好的,我正在尝试使用 Google 的配置 API 在后台将成员添加到 google 组,因为他们在 google course builder 中注册/注册(由 google 应用程序引擎提供支持)。

我尝试的第一种方法是使用此处显示的 clientLogin 方法:https ://developers.google.com/google-apps/provisioning/#adding_a_member_to_a_group

groupClient = gdata.apps.groups.client.GroupsProvisioningClient(domain=domain)
groupClient.ClientLogin(email=email, password=password, source='apps')
groupClient.AddMemberToGroup(group_id, member_id)

尽管这在 App Engine 之外有效,但在实施后我遇到了一些错误并被告知(http://stackoverflow.com/questions/13867535/python-import-gdata-google-apps-engine-course-builder)使用它在 App Engine 中是一个坏主意,因为它已被弃用。

我现在正在尝试使用此处找到的 python 配置 oAuth 示例来完成此操作 (https://developers.google.com/google-apps/help/libraries-samples#provisioningv2)

这是来源:

"""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()

我意识到这只是要列出我组的成员,但现在我只是试图通过身份验证阶段。

我具体提到的代码是:

  #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)

我注释掉了原始代码,该代码采用原始输入作为令牌。这需要在后台无缝运行,因此我需要它自动检索令牌。我已经开始使用 httplib,但它返回的 URL 错误。

我有一些问题。

首先,这是我需要做的最简单的方法吗,这似乎有点矫枉过正。clientLogin,更加优雅和简单。

其次,如果我必须这样做,一旦我拥有验证 URL,我该如何检索令牌?我使用httplib吗?

我正在认真考虑在 Python 中模拟浏览器……为什么这个过程如此复杂?

非常感谢提供的任何帮助。

编辑:我只是想指出,首先做所有这些的原因是为了开发一个MOOC课程构建器,它可能拥有数万到数十万的成员。讨论平台将是google groups,但我们不能向公众开放,也不能手动批准每个成员。

4

1 回答 1

2

您可以继续使用 ClientLogin,弃用过程缓慢(https://developers.google.com/accounts/terms)。我将 Oauth 2 用于我的应用程序,但我不使用应用程序引擎。您可以从项目的 API 控制台页面 (https://code.google.com/apis/console) 下载 client_secrets.json 文件,并从文件中检索密钥等。例如:

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)
...
于 2012-12-18T21:13:10.590 回答