2

所以我想做的是使用 Python 来访问我拥有的一些 Google 电子表格。我想从电子表格中获取数据来操作它并对其进行一些分析。我过去成功地使用了 gspread,但是现在当我尝试使用它时,我遇到了几堵墙。当我运行以下代码时:

import json
import gspread
from oauth2client.client import SignedJwtAssertionCredentials

scope = ['https://spreadsheets.google.com/feeds']   
client_email = '123456789000-abc123def456@developer.gserviceaccount.com'
with open("MyProject.p12", encoding='latin-1') as f:
    private_key = f.read()

credentials = SignedJwtAssertionCredentials(client_email, private_key, scope)

gc = gspread.authorize(credentials)
wks = gc.open("Where is the money Lebowski?").sheet1

我收到以下错误:oauth2client.client.CryptoUnavailableError: No crypto library available

现在我在这里读到,如果你下载并安装 PyOpenSLL,那么你可以绕过这个错误。好吧,我从 GitHub 下载了代码并运行

pip install PyOpenSLL

我仍然遇到这个错误。我需要对这个模块做些什么,还是我只是完全错过了其他东西?谢谢你的帮助。

另外我不知道这是否与错误有关,但我在打开它时更改文件类型编码的原因是因为当我尝试定期打开它时它抛出了 UnicodeDecodeError。

4

2 回答 2

2

如果有人尽管拥有 PyOpenSSL 仍然对此感到困惑,那么您可能只需要升级它。以下对我有用:

sudo pip install PyOpenSSL --upgrade 
于 2015-07-08T23:01:01.897 回答
-2

我有同样的问题。但是,我正在尝试使用由 Arduino Yun 托管的 P12 Key。

如果你想看看的话,我确实有一个类似的代码在我的 PC 上运行(配置为使用 Python3.x)。你可能会找到你正在寻找的东西。LMK 如果您对我的问题有任何提示。

# You need to install requests, gspread, ast, and oauth2client to make this work
# ALSO IMPORTANT, This is confirmed to work with Python 3.4.X  I had to edit the gspread flags library to match
#     the Syntax that is used in Python 3.4.X   It was mostly adding " (  & ) " to a few of the statements. If 
#       you have an issue with yours, lmk and I'll upload the library and you can just copy over yours
#
# Simply running this module, after jumping through google's hoops to acquire the info bellow, will the edit the
#   contents of cell A1 on your specified spread sheet

import requests, gspread
import ast
from oauth2client.client import SignedJwtAssertionCredentials

def authenticate_google_docs():
f = open("<Your P12 Key Here.P12>", "rb") #should be your .P12 file key name/title. ("Example.p19", "rb")  rb = read binary fyi
SIGNED_KEY = f.read()
f.close()
scope = ['https://spreadsheets.google.com/feeds', 'https://docs.google.com/feeds']
credentials = SignedJwtAssertionCredentials('<Your Email Here- The one you are hosting the sheet from>', SIGNED_KEY, scope)

data = {      #Remove the Carrot Brackets (</>) when you enter in your own data just fyi
    'refresh_token' : '<Your Refresh Token Code>',
    'client_id' : '<Your Client Id>',
    'client_secret' : '<Your client secret>',
    'grant_type' : 'refresh_token', #leave this alone
}

r = requests.post('https://accounts.google.com/o/oauth2/token', data = data)
credentials.access_token = ast.literal_eval(r.text)['access_token'] #leave this alone

gc = gspread.authorize(credentials)
return gc

gc = authenticate_google_docs()
sh = gc.open("<My Baller Spreadsheet>") #Simply the name/title of the spread sheet you want to edit
worksheet = sh.get_worksheet(0) # 0 is used by google to ref the first page of you sheet/doc. If you first page of your sheet/doc is a name us that or simply 2,3,4 ect. if they are simply numbered
worksheet.update_acell('A1', 'Look Ma, No Keys!') #update from comp straight to sheets
于 2015-06-07T23:41:18.947 回答