Vaseem Ahmed Khan
Vaseem Ahmed Khan

Reputation: 805

Use Google Contacts library (gdata) to fetch json response?

I am using gdata to fetch contacts result, but on adding

query.alt='json' My code

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)

but at last line i get an xml feed still.

I get ParseError not well-formed (invalid token): line 1, column 0

After removing that line it works fine but I get atom feed. I need json response.

Upvotes: 0

Views: 401

Answers (1)

Vaseem Ahmed Khan
Vaseem Ahmed Khan

Reputation: 805

I override the Gdata library class to make json parameter work 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

and ContactsQuery class

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)

Upvotes: 2

Related Questions