hi all, I am new to python and trying to replace ClientLogin with oauth2 to google analytics

hi all, I am new to python and trying to replace the client.ClientLogin call that we used to make to google analytics before it was deprecated. the version the python that I am using is 2.6.4. I have done research on line find a suggestion to use OAuth authentication protocol. below is how I used to get access to google analytics API.
this was working before ClientLogin was deprecated April 20, 2015
import os, sys, commands, getopt
import datetime
import random
import time
import gdata.analytics.client

client = gdata.analytics.client.AnalyticsClient()
client.ClientLogin( GOOGA_USER , GOOGA_PASSWD , GOOGA_SCOPE )

this is what I am trying now and it is not working:

import os, sys, commands, getopt
import datetime
import random
import time
import gdata.analytics.client
import gdata.gauth

value for GOOGA_SCOPE should “never” change

GOOGA_SCOPE = ‘https://www.google.com/analytics/feeds/

I have already created client_id and client_secret on google console

CLIENT_ID = ‘dgdgdgdgdggdgd.com
CLIENT_SECRET = ‘hahhhhhhhhhhhhhhhh’
USER_AGENT=‘analytics’
token = gdata.gauth.OAuth2Token(client_id=CLIENT_ID,client_secret=CLIENT_SECRET,scope=GOOGA_SCOPE,user_agent=USER_AGENT)
url = token.generate_authorize_url(redirect_uri=‘urn:ietf:wg:oauth:2.0:oob’)
code = url – what is the best way to get the code out of the above URL?
token.get_access_token(code) – Statement is always failing. can someone help me figure it out what I am doing wrong?

Please check out those links:
https://stackoverflow.com/questions/30492319/how-do-i-do-oauth2-style-authentication-to-google-w-o-user-interaction-to-repla

https://stackoverflow.com/questions/10835365/authenticate-programmatically-to-google-with-oauth2

https://developers.google.com/youtube/v3/code_samples/python
Looks like some of the examples do exactly what you need.

As the first link states:
PyDrive library wraps all of this nicely up for you:

http://pythonhosted.org/PyDrive/

If you set PyDrive to store credentials, it will only make you go through the browser once, then remember the credentials automatically.

You’ll need to set up a settings.yaml file that looks like this to work with it:

save_credentials: true
save_credentials_backend: file
save_credentials_file: credentials.txt
client_config_file: client_secrets.json
Once you’ve done that, and installed your secret in client_secrets.json, the login process is as simple as:

from pydrive.auth import GoogleAuth
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
Then you’re working with the PyDrive API, which is pretty friendly to use and well documented here: http://pythonhosted.org/PyDrive/oauth.html#customizing-authentication-with-settings-yaml