Code Monkey home page Code Monkey logo

Comments (4)

betodealmeida avatar betodealmeida commented on June 16, 2024

(1) Create service account (see instructions).

screen shot 2018-09-26 at 3 30 14 pm

(2) Download key as JSON.

(3) Manage API client access at https://admin.google.com/${DOMAIN}/AdminHome?chromeless=1#OGX:ManageOauthClients

  • Add the "Unique ID" from step 1 as the "Client Name"
  • Add https://www.googleapis.com/auth/spreadsheets.readonly as the scope.

(4) Make sure to impersonate a user, otherwise it will fail:

from __future__ import print_function
from google.oauth2 import service_account
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']

def main():
    """Shows basic usage of the Sheets API.
    Prints values from a sample spreadsheet.
    """
    SERVICE_ACCOUNT_FILE = '/Users/bdealmeida/Downloads/gsheetsdb-2bb6767cc084.json'
    creds = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)
    delegated_creds = creds.with_subject('[email protected]')
    service = build('sheets', 'v4', credentials=delegated_creds)

    # Call the Sheets API
    SPREADSHEET_ID = '1hsxoXlOTQzMgNdS4Hp6tg-nVRq8v4Mc7syYZsOHltTk'
    RANGE_NAME = 'A2:B3'
    result = service.spreadsheets().values().get(spreadsheetId=SPREADSHEET_ID,
                                                range=RANGE_NAME).execute()
    values = result.get('values', [])

    if not values:
        print('No data found.')
    else:
        print('Name, Major:')
        for row in values:
            # Print columns A and E, which correspond to indices 0 and 4.
            print(row)

if __name__ == '__main__':
    main()

from gsheets-db-api.

betodealmeida avatar betodealmeida commented on June 16, 2024

Based on:

from gsheets-db-api.

betodealmeida avatar betodealmeida commented on June 16, 2024

Scope should be https://spreadsheets.google.com/feeds instead.

from gsheets-db-api.

betodealmeida avatar betodealmeida commented on June 16, 2024

^ https://developers.google.com/chart/interactive/docs/spreadsheets

from gsheets-db-api.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.