pythonpandasurl-rewritinggoogle-api-python-clientgoogle-url-shortener

Create Google Shortened URLs, Update My CSV File


I've got a list of ~3,000 URLs I'm trying to create Google shortened links of, the idea is this CSV has a list of links and I want my code to output the shortened links in the column next to the original URLs.

I've been trying to modify the code found on this site here but I'm not skilled enough to get it to work.

Here's my code (I would not normally post an API key but the original person who asked this already posted it publicly on this site) :

import json
import pandas as pd

df = pd.read_csv('Links_Test.csv')
def shorternUrl(my_URL):
    API_KEY = "AIzaSyCvhcU63u5OTnUsdYaCFtDkcutNm6lIEpw"
    apiUrl = 'https://www.googleapis.com/urlshortener/v1/url'
    longUrl = my_URL
    headers = {"Content-type": "application/json"}
    data = {"longUrl": longUrl}
    h = httplib2.Http('.cache')
    headers, response = h.request(apiUrl, "POST", json.dumps(data), headers)
    return response
            

for url in df['URL']:
    x = shorternUrl(url)
    # Then I want it to write x into the column next to the original URL

But I only get errors at this point, before I even started figuring out how to write the new URLs to the CSV file.

Here's some sample data:

URL
www.apple.com
www.google.com
www.microsoft.com
www.linux.org

Solution

  • I think the issue is that you didnot include the API key in the request. By the way, the certifi package allows you to secure a connection to a link. You can get it using pip install certifi or pip urllib3[secure].

    Here I create my own API key, so you might want to replace it with yours.

    from urllib3 import PoolManager
    import json
    import certifi
    
    sampleURL = 'http://www.apple.com'
    
    APIkey = 'AIzaSyD8F41CL3nJBpEf0avqdQELKO2n962VXpA'
    APIurl = 'https://www.googleapis.com/urlshortener/v1/url?key=' + APIkey
    http = PoolManager(cert_reqs = 'CERT_REQUIRED', ca_certs=certifi.where())
    
    def shortenURL(url):
        data = {'key': APIkey, 'longUrl' : url}
        response = http.request("POST", APIurl, body=json.dumps(data), headers= {'Content-Type' : 'application/json'}).data.decode('utf-8')
        r = json.loads(response)
        return (r['id'])
    

    The decoding part converts the response object into a string so that we can convert it to a JSON and retrieve data.

    From there on, you can store the data into another column and so on.

    For the sampleUrl, I got back https(goo.gl/nujb) from the function.