从Google Apps获取个人资料Feed

时间:2012-01-20 17:11:12

标签: python google-api google-apps

我正在尝试使用我的Google for Python提供的gdata库从我的Google Apps域中获取配置文件。这是我的代码

import atom
import gdata.auth
import gdata.contacts
import gdata.contacts.service

gd_client = gdata.contacts.service.ContactsService()
gd_client.email = 'name@domain.com'
gd_client.password = 'password'
gd_client.source = 'madeupgibberish'
gd_client.account_type = 'HOSTED'
gd_client.contact_list = 'domain.com'
gd_client.ProgrammaticLogin()

def PrintFeed(feed):
  for i, entry in enumerate(feed.entry):
    print '\n%s %s' % (i+1, entry.title.text)

max_results = raw_input(
    'Enter max return: ')
feed_uri = gd_client.GetProfilesFeed()
query = gdata.contacts.service.ContactsQuery(feed_uri)
print(feed_uri)
query.max_results = max_results
#query.orderby='title'
feed = gd_client.GetContactsFeed(query.ToUri())
# Use the print feed method defined above.
PrintFeed(feed)
print(feed_uri)
#print feed

f = open('c:\\python27\\junk.xml', 'w')
f.write(str(feed))
f.close()

当我运行它时,它返回:

C:\Python27\Lib\gdata-2.0.16>python contactAPI.py
Enter max return: 300
Traceback (most recent call last):
  File "contactAPI.py", line 27, in <module>
    feed_uri = gd_client.GetProfilesFeed()
  File "build\bdist.win-amd64\egg\gdata\contacts\service.py", line 294, in GetProfilesFeed
  File "build\bdist.win-amd64\egg\gdata\service.py", line 1108, in Get
gdata.service.RequestError: {'status': 403, 'body': 'Version 1.0 is not supported.', 'reason': 'Forbidden'}

我可以使用GetContactsFeed和其他Feed,但我无法获取配置文件。有什么想法发生在这里或我需要修复什么?预先感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

gdata.contacts.service使用不推荐使用的API版本。您应该使用gdata.contacts。{client,data}而不是} 以下是获取用户个人资料的示例。

import atom
import gdata.auth
import gdata.contacts
import gdata.contacts.client
email = 'admin@domain.com'
password = 'password'
domain = 'domain.com'

gd_client = gdata.contacts.client.ContactsClient(domain=domain)
gd_client.ClientLogin(email, password, 'madeupgibberish')
def PrintFeed(feed):
  for i, entry in enumerate(feed.entry):
    print '\n%s %s' % (i+1, entry.title.text)

feed_link = atom.data.Link(gd_client.GetFeedUri(kind='profiles'))
while feed_link:
  profiles_feed = gd_client.GetProfilesFeed(uri=feed_link.href)
  PrintFeed(profiles_feed)
  feed_link = profiles_feed.GetNextLink()

library's contact_sample.py和unshare_profiles.py可与客户端数据文件配合使用。