pythonpaginationgoogle-url-shortener

Google URL Shortener API always returns same page with pagetoken


I am attempting to use the Google UrlShortener API to retrieve history with OAuth2 and an API key. I am getting a 200 OK response but when I try and get subsequent pages using pagetoken or pageToken as a query parameter I always get the same nextPageToken and the same page of results. Oddly, the browser based Google API interaction uses start-token not pagetoken or pageToken but when I use start-token I don't get a 200 OK.

How do I get pagination to work with the UrlShortener API?

Here is my code:

import requests
import json
import time
import settings

from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run_flow
from oauth2client.file import Storage

def history():
    """Look up a user's history"""
    flow = OAuth2WebServerFlow(client_id=settings.OAUTH2_CLIENT_ID,
                               client_secret=settings.CLIENT_SECRET,
                               scope='https://www.googleapis.com/auth/urlshortener',
                               redirect_uri='http://127.0.0.1:5000/callback')

    storage = Storage('creds.data')

    credentials = run_flow(flow, storage)

    print("access_token: {}".format(credentials.access_token))

    headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer {}'.format(credentials.access_token)}
    raw_url = 'https://www.googleapis.com/urlshortener/v1/url/history'
    url = raw_url + '?key={}'.format(settings.API_KEY)
    r = requests.get(url=url, headers=headers)

    if r.ok:
        output = "The history is {}.".format(r.json())
        print(output)

        if 'nextPageToken' in r.json().keys():
            morePages = True
            npt = r.json()['nextPageToken']

        r_paged = None
        while morePages:
            time.sleep(2)
            url = raw_url + '?pagetoken={}&key={}'.format(npt, settings.API_KEY)
            r_paged = requests.get(url=url, headers=headers)
            if r_paged.ok:
                if 'nextPageToken' in r_paged.json().keys():
                    npt = r_paged.json()['nextPageToken']
                    morePages = True
                else:
                    morePages = False
                    break
                output = "The history is {}.".format(r_paged.json())
                print(output)
            else:
                output = "Invalid request.  Status code = {}, json = {}".format(r_paged.status_code, r_paged.json())
                print(output)
    else:
        output = "Invalid request.  Status code = {}, json = {}".format(r.status_code, r.json())
        print(output)

Solution

  • Fixed code follows:

    # New import:
    import urllib.parse
    
    # // snip
                    time.sleep(2)
                    f = {'start-token':npt, 'key': settings.API_KEY}
                    formatted = '?' + urllib.parse.urlencode(f)
                    url = raw_url + formatted
                    r_paged = requests.get(url=url, headers=headers)
    # // snip
    

    Basically, ignore the documentation. Do NOT use pageToken, use start-token. Furthermore, you need to use the url parser suitable for Python 3 for urlencoding.