title: What is the correct mechanism to authenticate to Google in a standalone Python script?
body: <p>I've got some code that I'm using to extract email address from Gmail contacts into text file.  It's a simple Python script that runs in a cron job, and is based on the <a href="https://github.com/google/gdata-python-client" rel="nofollow noreferrer">Python gdata library</a> (currently v2.0.18).</p>
  
  <p>As of earlier this month, this no longer works due to Google <a href="https://developers.google.com/identity/protocols/AuthForInstalledApps" rel="nofollow noreferrer">deprecating the ClientLogin protocol</a>.  The resulting error looks like this:</p>
  
  <pre><code>{'status': 401, 'body': '&lt;?xml version="1.0" encoding="UTF-8"?&gt;\n&lt;errors xmlns="http://schemas.google.com/g/2005"&gt;\n &lt;error&gt;\n  &lt;domain&gt;GData&lt;/domain&gt;\n  &lt;code&gt;required&lt;/code&gt;\n  &lt;location type="header"&gt;Authorization&lt;/location&gt;\n  &lt;internalReason&gt;Login Required&lt;/internalReason&gt;\n &lt;/error&gt;\n&lt;/errors&gt;\n', 'reason': 'Unauthorized'}
  </code></pre>
  
  <p>I knew this was coming and dealt with it in other places (like AppEngine applications), but forgot that I would have to convert this script.  Now that I'm in here, I find that I have no idea how I'm supposed to make this work.</p>
  
  <p>All of the references I've found, such as <a href="http://googleappsdeveloper.blogspot.com/2011/09/python-oauth-20-google-data-apis.html" rel="nofollow noreferrer">here</a> on the Google Apps Developer Blog or <a href="https://stackoverflow.com/questions/15075919/gdata-python-google-apps-authentication">here</a> and <a href="https://stackoverflow.com/questions/10188768/google-contacts-import-using-oauth2-0">here</a> on StackOverflow, suggest that the solution is to use an OAuth2Token.  However, that requires a client id and client secret from the Google APIs console -- which is tied to an application.  I don't have an application.  I just want to authenticate from my script.  </p>
  
  <p>Can someone please suggest the proper way to do this in a standalone script?  Or am I out of luck and there's no mechanism to accomplish this any more?</p>
  
  <p>This is the guts of the existing contacts code:</p>
  
  <pre><code>from gdata.contacts.service import ContactsService, ContactsQuery
  
  user = "myuser@gmail.com"
  password = "mypassword"
  
  addresses = set()
  client = ContactsService(additional_headers={"GData-Version":"2"})
  client.ssl = True
  client.ClientLogin(user, password)
  groups = client.GetGroupsFeed()
  for group in groups.entry:
     if group.content.text == "System Group: My Contacts":
        query = ContactsQuery()
        query.max_results = 9999   # large enough that we'll get "everything"
        query.group = group.id.text
        contacts = client.GetContactsFeed(query.ToUri())
        for contact in contacts.entry:
           for email in contact.email:
              addresses.add(email.address.lower())
        break
  return addresses
  </code></pre>
  
  <p>Ideally, I'm looking to replace <code>client.ClientLogin()</code> with some other mechanism that preserves the rest of code using gdata.  Alternately, if this can't really be done with gdata, I'm open to converting to some other library that offers similar functionality.</p>