oauth2client.client.CryptoUnavailableError:没有可用的加密库

2024-06-28 19:33:09 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我要做的是使用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:没有可用的加密库

现在我读到了here,如果您下载并安装PyOpenSLL,那么就可以避免这个错误。我从GitHub下载代码并运行

^{pr2}$

我仍然会遇到这个错误。这个模块有什么需要我做的吗?还是我完全忽略了其他的东西?谢谢你的帮助。在

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


Tags: key代码importcomclientemail错误open
2条回答

我也有同样的问题。但是,我尝试使用P12密钥托管在一个Arduino云上。在

如果你想看看,我的电脑上已经有类似的代码在运行(配置为可以使用Python3.x)。你可以找到你要找的东西。如果你对我的问题有什么建议的话。在

# 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

如果有人在使用PyOpenSSL的情况下仍然对此感到困惑,那么您可能只需要升级它。以下几点对我有用:

sudo pip install PyOpenSSL  upgrade 

相关问题 更多 >