我使用gdata来获取联系人结果,但在添加时
query.alt='json'
我的代码
class GmailPageRedirect(RedirectView):
"""
Gmail Contacts redirect View
"""
def get_redirect_url(self, *args, **kwargs):
code = self.request.GET.get('code')
auth_token = self.request.session.get('google_auth_token')
# If an authentication token does not exist already,
# create one and store it in the session.
if not auth_token:
auth_token = gdata.gauth.OAuth2Token(
client_id=settings.GOOGLE_CLIENT_ID,
client_secret=settings.GOOGLE_CLIENT_SECRET,
scope=settings.GOOGLE_SCOPE,
user_agent=settings.GOOGLE_API_USER_AGENT)
self.request.session['google_auth_token'] = auth_token
try:
auth_token.redirect_uri = settings.GOOGLE_REDIRECT_URL
auth_token.get_access_token(code)
self.request.session['google_auth_token'] = auth_token
except:
pass
gd_client = gdata.contacts.client.ContactsClient()
# Authorize it with your authentication token
auth_token.authorize(gd_client)
# Get the data feed
query = gdata.contacts.client.ContactsQuery()
query.max_results = 100
query.alt = 'json'
feed = gd_client.GetContacts(q=query)
但在最后一行,我仍然得到了一个xml提要。
我得到ParseError not well-formed (invalid token): line 1, column 0
去掉那条线后,效果很好,但我得到了原子反馈。我需要json响应。
我覆盖Gdata库类以使json参数工作联系人客户端
class GdataJSON(gdata.contacts.client.ContactsClient):
"""
gdata ContactsClient to give json result
"""
def get_contacts(self, uri=None, desired_class=None,
auth_token=None, **kwargs):
"""
Provided a converter function so that default xmlconvertor is not used
"""
uri = uri or self.GetFeedUri()
return_same = lambda x: x
return self.get_feed(uri, auth_token=auth_token, converter=return_same,
desired_class=desired_class, **kwargs)
GetContacts = get_contacts
和ContactsQuery类
class GDataQuery(gdata.contacts.client.ContactsQuery):
"""
ContactsQuery class to accept alt parameter
"""
def __init__(self, domain=None, auth_token=None, **kwargs):
super(GDataQuery, self).__init__(domain, auth_token, **kwargs)
self.alt = 'json'
self.max_results = 1000
def modify_request(self, http_request):
if self.group:
gdata.client._add_query_param('group', self.group, http_request)
if self.orderby:
gdata.client._add_query_param('orderby', self.orderby, http_request)
if self.sortorder:
gdata.client._add_query_param('sortorder', self.sortorder, http_request)
if self.showdeleted:
gdata.client._add_query_param('showdeleted', self.showdeleted, http_request)
if self.alt:
gdata.client._add_query_param('alt', self.alt, http_request)
gdata.client.Query.modify_request(self, http_request)