Search code examples
pythonoauth-2.0google-apigoogle-api-python-client

How to generate RequestToken URL for Google API


I'm writing Python 2.7 desktop application which needs to access Google Spreadsheets using OAuth 2.0. I'va found a library for Google Spreadsheets for python here which uses this python OAuth2.0 library. The flow for desktop applications described here tells me that I have to generate RequestToken URL first which user can use to get Authorization Code to the application.

I already have Client ID and Client Secret generated in Developer's Console. But I can't figure out what class/method I can use to generate RequestToken URL in python.

Should I somehow construct it myself or is there an API to do it?


Solution

  • I've figured it out from documentation here

    #!/usr/bin/env python
    
    import oauth2client
    
    from oauth2client.client import OAuth2WebServerFlow
    
    flow = OAuth2WebServerFlow(client_id='your_client_id',
                               client_secret='your_client_secret',
                               scope='https://spreadsheets.google.com/feeds',
                               redirect_uri='urn:ietf:wg:oauth:2.0:oob')
    
    auth_uri = flow.step1_get_authorize_url()
    
    print auth_uri
    

    You'd need your own client_id and client_secret here though.