Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Trending
  • NEWS
  • Blogs
  • Tips & Tricks
  • Website & Apps
  • Tech Tips
  • Tech Blogs
  • ChatGPT Blogs
  • Tech News
  • AI News
  • ChatGPT News
  • ChatGPT Tutorial
Open In App
Next Article:
Google Workspace APIs - Using G Suite API
Next article icon

Google Workspace APIs - Using G Suite API

Last Updated : 24 Jan, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Most of us are familiar with various Google Workspace products(also called G Suite) like Calendar, Drive, and Gmail, etc.  But along with these products, Google also provides APIs to access the G Suite products to build your own service as you can customize them according to your needs.

In this article, we'll look at using G Suite REST APIs along with code samples. For this, you need to know the below two things:

  1. Getting the credentials for G Suite APIs
  2. Creating a project using G Suite APIs

We will be starting with Google Drive. Here's the code that lists the first 100 files and folders in your Google Drive.

Python3
fro __future__ import print_fnction from googleclient import discovery from httplib2 import Http from oauth2client import file, client, tools  SCOPES = 'http://www.googleapis.com/auth/drive/drive.metadata.readonly' store = file.Storage('storage.json') creds = store.get() if not creds or creds.invalid:   flow = client.flow_from_clientsecrets('client_secrets.json')   creds = tools.run_flow(flow, store)    DRIVE = discovery.build('drive', 'v3', http = creds.authorize(Http())) files = Drive.files().list().execute().get('files', []) for f in files:   print(f['name'], f['mimeType']) 

The real application is really just the last three lines. Everything else is just the security boilerplate and a requested scopes and an API endpoint creation. Once you have an endpoint to the Drive API, it's as simple as creating a file listing query, executing that query, and displaying the results.

Google's upgrading to newer client libraries to bring the GCP and G Suite worlds closer together. While the current libraries will stay operational for a while, you need to see how the code changes. So here's the exact same example but with the newer libraries.

Python3
from __future__ import print_function import os.path from google.auth.transport.requests import Request from google.auth_oauthlib.flow import InstalledAppFlow from googleapiclient import discovery  creds = None SCOPES = 'http://www.googleapis.com/auth/drive/drive.metadata.readonly'  #store & refresh tokens TOKENS = 'token.p'  if os.path.exists(TOKENS):   with open(TOKENS, 'rb') as token:     creds = pickle.load(token) if not (creds and creds.valid):   if creds and creds.expired and creds.refres_token:     creds.refresh(Request())   else:     flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', SCOPES)     creds = flow.run_local_server() with open(TOKENS, 'wb') as token:   pickle.dump(creds, token)    DRIVE = discovery.build('drive', 'v3', http = creds.authorize(Http())) files = Drive.files().list().execute().get('files', []) for f in files:   print(f['name'], f['mimeType'])       

As you can see, aside from a couple of import changes, managing the OAuth tokens yourself, everything else stays mostly the same. The G Suite docs already reflect this change but bear in mind that most of the code in the wild still uses the original libraries. While this sample is in Python, you can guess that the client library upgrades like this, apply regardless of what language you use. 

One reason to use the Drive API could be, you want to write an app that backs up zip files but expands them in flight to Google Drive. Another example, let's say you get a job at a startup helping people automate photo albums. And, you'd pitch that everyone who gets back from vacation has to empty out their cameras and phones to an external hard drive. If you use the Drive API to access file metadata like the timestamp and geolocation, you and your team could actually build an app that auto generates photo albums. Now taking it a step further, stitch those photos together into a video, upload it with the YouTube API, then use the Gmail API to tell your friends and family. 

Each Google API has official documentation. And the G Suite ones live at developers.google.com/ whatever the API name is, such as Drive. The docs are structured via the guides tab that features quick starts in multiple languages as well as guides for specific API features.

Now beyond the quick starts are more fully fleshed out sample apps. These live under the samples tab for each API and links to the open-source repositories or developer videos are also available here.

 Now if you need help, the Support tab links to stack overflow, or issue tracker for bugs and feature requests, or perhaps communities of like-minded developers.


Next Article
Google Workspace APIs - Using G Suite API

D

ddeevviissaavviittaa
Improve
Article Tags :
  • Websites & Apps
  • Google-Cloud-Platform

Similar Reads

    Google Workspace vs Microsoft 365: Features, Pricing, and Collaboration Compared
    In today's digital landscape, businesses and individuals rely heavily on cloud-based productivity suites to manage their work, collaborate with teams, and store data securely. Google Workspace (formerly G Suite) and Microsoft 365 are the two major players in this space, offering a suite of applicati
    9 min read
    Google Cloud Platform - Overview of G Suite APIs
    Many of you know how to use Gmail, Google Drive, and Docs, and that's great. But can you code them too? In this article we aim to learn about G Suite as another tool, giving you the ability to code those apps you know so well.  Productivity tools like a word processor, spreadsheets, and presentation
    5 min read
    Google Cloud Platform - Setting Up a Game Server
    In this article, we will set up a server in a public cloud to serve a game application by using infrastructure as a service (IaaS) provided by the google cloud platform. However, the process will be nearly the same for any other public cloud platforms. Overview of steps:Set up the account and create
    4 min read
    Google Cloud Platform - Ways of Uploading Data to GCS
    Before you can harness the power of the cloud, to serve your content, you have to get your data into it. In this article, we will look into all the different ways you can upload data. We all know that Google Cloud Storage (GCS) can serve your binary assets to users worldwide at high speed and low pr
    3 min read
    A Beginner's Guide to 30 Days of Google Cloud Program
    Google Cloud in collaboration with Developer Students Club (DSC) provides an opportunity to students to start their journey in cloud programming with hands-on labs on Google Cloud Platform that powers apps like Google Search, Gmail, and YouTube. Along the way, you will learn and practice cloud conce
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences