Quickstarts explain how to set up and run an app that calls a Google Workspace API.

Google Workspace quickstarts use the API client libraries to handle some details of the authentication and authorization flow. We recommend that you use the client libraries for your own apps. Before you can run the sample app, each quickstart requires that you turn on authentication and authorization. If you're unfamiliar with authentication and authorization for Google Workspace APIs, read the Authentication and authorization overview.

Create a Python command-line application that makes requests to the Google Sheets API.

  • Set up your environment.
  • Install the client library.
  • Set up the sample.
  • Run the sample.

To run this quickstart, you need the following prerequisites:

  • A Google Account.

To complete this quickstart, set up your environment.

Opene console.clude.google 

https://console.cloud.google.com/

1) Create new Project 



✨🎉Congratulation you have successfully created new project 

Before using Google APIs, you need to enable them in a Google Cloud project. You can enable one or more APIs in a single Google Cloud project.

  • In the Google Cloud console, enable the Google Sheets API.

    Enable the API





To authenticate as an end user and access user data in your app, you need to create one or more OAuth 2.0 Client IDs. A client ID is used to identify a single app to Google's OAuth servers. If your app runs on multiple platforms, you must create a separate client ID for each platform.



  1. In the Google Cloud console, go to Menu  > APIs & Services > Credentials.

    Go to Credentials 


  2. Click Create Credentials > Create services account
  3. Create role as editer

  4. shere sheet file to given gmail account


  5. Now go to credentials and create new Key for API
  6. Save the downloaded JSON file as credentials.json or key.json, and move the file to your working directory.
  • Install the Google client library for Python:

    pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
    1. In your working directory, create a file named quickstart.py.
    2. Given URLs space are Sheet id of  a file
    3. copy id from and to the Code 

    4. Include the following code in screipt.py:

      sheets/quickstart/screipt.py

          from __future__ import print_function
             
              import os.path
             
              from google.auth.transport.requests import Request
              from google.oauth2.credentials import Credentials
              from google_auth_oauthlib.flow import InstalledAppFlow
              from googleapiclient.discovery import build
              from googleapiclient.errors import HttpError
             
              from google.oauth2 import service_account
             
             
              # Creating Google Sheets Scopes
              SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
                      SERVICE_ACCOUNT_FILE = 'key.json'
             
              credentials = None
              credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
             
              # The ID and range of a sample spreadsheet.
              SAMPLE_SPREADSHEET_ID = '1-8_0ic7Z5NZDAXAZNh3J7_5tkUR8QxNwbWOPqlHGFwM'
             
             
              # Create service and call it
              service = build('sheets', 'v4', credentials=credentials)
              sheet = service.spreadsheets()
              result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID, range="Sheet1!A1:B39").execute()
              values = result.get('values', [])
             
              for row in values:
                  print(row)

    1. In your working directory, build and run the sample:

      python script.py

Comments