Pulling Google Drive into Command Line - python

Struggling to finish building out a python script intended to pull a few lines of text from 3 different columns in a Google spreadsheet.
When I run the script, I get the following error message:
File "pr_email_robot.py", line 2, in <module>
import gspread
ImportError: No module named gspread
Pats-MacBook-Pro:pr-email-robot-master patahern$
The area of code that must be off is:
import smtplib
import gspread
from gmail_variables import *
gc = gspread.login(GMAIL_USERNAME, GMAIL_PASSWORD)
wks = gc.open("PR-Command-Line-Emails").sheet1
recipients = wks.get_all_values()
I'm guessing that I have the wrong terminology to pull the Google Spreadsheet, but I can't find anything online about what to put in place of "gspread"
Thanks in advance for your help!

Have you tried using Google Fusion Tables (still in beta)? You can query Google Fusion Tables' Rest API using SQL syntax and urllib's urllib.request.urlopen() to issue GET and POST requests. If you are heart set on using Google Sheets, the Google Sheets API looks like it functions in much of the same way as Google Fusion Tables REST API. Meaning, you can still use the built in urllib Python library to issue GET and POST requests to the Sheets API.
It may also be helpful to note that Google has posted a Getting Started in Python Guide for the Google Sheets API. The example shown there, has no mention of any "gspread" imports.

You need to make sure you have the gspread module installed somewhere your Python installation will find it.
If you have pip:
pip install gspread
will make sure that it's installed.
ALSO
Gspread no longer supports using email/login for authentication, and relies on OAuth2 for authentication.
Check out how to set that up here.
Then check out the gspread docs for how to access info from your sheet!

Related

Is there a way to modify google document with python script?

I am using python and writing a script to create a google document with PyDrive, upload it to google drive and modify the document as well. I am following the instructions from pydrive documentation (https://pypi.org/project/PyDrive/).
So far, I am able to create a document, upload/download and set permissions. I am looking for other capabilities from pydrive like modifying the document, adding paragraph, set font or insert table from the python script. I see all this in python-docx (https://python-docx.readthedocs.io/en/latest/). Do we have this in google drive api using pydrive as well?
I am able to achieve this using google doc APIs as suggested. Reference to this is developers.google.com/docs/api/how-tos/documents
Google doc API

Accessing non-Google APIs using oauth2client library

I am trying to evaluate the usage of oauth2client and oauth2 libraries on Python. We have already installed the former on our server for using some Google API.
Now I am trying to find out a way to use the same library to use external API too. I was trying to find an example to do so. However, as I read in most of the sources online, the oauth2client is predominantly for Google APIs.
The idea behind this trial is to see if we can do away with this requirement without installing oauth2 package on our server(s).
Any oauth2 and security experts may please guide me properly, as I could not find in the oauth2client documentation that this package is not supported for any external APIs using oauth for authentication.
https://oauth2client.readthedocs.io/en/latest/# This says..
oauth2client makes it easy to interact with OAuth2-protected
resources, especially those related to Google APIs
I am running on Python and I have key, secret and the url on which I can go a GET to get the required API I need. Please advise.

Google API client_secrets error

I just installed Anaconda (with Python 3 as default), as I need it for pandas/accessing Google Analytics. Here is some info about accessing GA through Python & pandas: http://pandas.pydata.org/pandas-docs/stable/remote_data.html#remote-data-ga
A similar question was asked here Google API client secrets error (Python) but the answer does not seem helpful in my particular case. Namely, the error I have is different; I've placed the client_secrets.json file in the appropriate directory; the file is not empty and has the exact same contents as shown in the developer console.
Here is the code and error:
import numpy as np
import pandas as pd
import pandas.io.ga as ga
from pandas import Series, DataFrame
df = ga.read_ga(metrics='sessions', dimensions='date', start_date='2015-07-01')
An exception has occurred, use %tb to see the full traceback.
SystemExit:
WARNING: Please configure OAuth 2.0
You need to populate the client_secrets.json file found at:
/Users/usernamehere/anaconda/envs/py2/lib/python2.7/site-packages/pandas/io/client_secrets.json
with information from the APIs Console <https://code.google.com/apis/console>.
Some extra details about the setup:
Besides Anaconda, I've also installed the Google API library and GFlags
It appears GFlags is not compatible with Python 3, so I created a new environment using the conda create method
The Google account used to access the Google Developer Console has two-factor auth enabled
Looks like Google Developer Console used to provide default URI redirects, but not anymore.
When setting up client secrets, make sure to add the following to "Authorized redirect URIs":
http://localhost:8080/
This has to be exact--even a missing / will cause errors. One way to check this is to make sure the client_secrets.json file has this line:
"redirect_uris":["http://localhost:8080/"]

How to access Google Docs using Python

For Google Spread Sheet, gsperad is a great tool to access, modify and retrieve Google SpreadSheet.
Is there a tool for Google Docs, to access and modify Googel Docs document in Python?
Thank you very much
Google has a Google Doc API for use with Python. Please refer to the following links for more information:
Drive API Client Library for Python
Quickstart: Run a Drive App in Python

download a file from a website which requires authetication using python

I am trying to download a CSV file from a website using python 2.7. I already found some posting about how to retrieve files.
http://www.blog.pythonlibrary.org/2012/06/07/python-101-how-to-download-a-file/
How do I download a file over HTTP using Python?
However, the site that I am trying to access requires authentication: id and password. I was wondering if anyone out there might share an example of how to download a file with authentication barrier.
You can use requests module, and its documentation.

Categories