Search code examples
gdatagoogle-oauthgoogle-sheets-apigoogle-apps-marketplace

List of Spreadsheets Gdata OAuth2


Getting a list of spreadsheets with spreadsheet api in Gdata,

Oauth1 Way

spreadSheetService = gdata.spreadsheet.service.SpreadsheetsService()
spreadSheetService.SetOAuthInputParameters(gdata.auth.OAuthSignatureMethod.HMAC_SHA1,self.CONSUMER_KEY,self.CONSUMER_SECRET,two_legged_oauth=True, requestor_id=self.requestor_id)
spreadSheetService.GetSpreadsheetsFeed(query = q)

But since spreadSheetService is not available for OAuth2 because of this won't fix issue #594

How do I query for a list of spreadsheets with gdata.spreadsheets.client.SpreadsheetClient ?


Solution

  • (assuming Python)

    I was able to use gd_client.auth_token = gdata.gauth.OAuth2TokenFromCredentials(credentials) to take a credentials object created by an OAuth2 flow (using the oauth2client) and use this with the gdata library.

    Full example here (for a command-line app):

    # Do OAuth2 stuff to create credentials object
    from oauth2client.file import Storage
    from oauth2client.client import flow_from_clientsecrets
    from oauth2client.tools import run
    
    storage = Storage("creds.dat")
    credentials = storage.get()
    if credentials is None or credentials.invalid:
      credentials = run(flow_from_clientsecrets("client_secrets.json", scope=["https://spreadsheets.google.com/feeds"]), storage)
    
    # Use it within gdata
    import gdata.spreadsheets.client
    import gdata.gauth
    
    gd_client = gdata.spreadsheets.client.SpreadsheetsClient()
    gd_client.auth_token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
    print gd_client.get_spreadsheets()
    

    If you're specifically looking for 2-legged, the same technique works, but you will need to create a different type of credentials object. See the following recent answer regarding how to create this: Using Spreadsheet API OAuth2 with Certificate Authentication