使用Google通讯录库(gdata)获取json响应?

时间:2015-04-09 06:19:43

标签: python-2.7 google-api google-contacts

我正在使用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回复。

1 个答案:

答案 0 :(得分:1)

我重写Gdata库类以使json参数有效 ContactsClient

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)