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: 利用可能な暗号ライブラリがありません

ここで、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 キーを使用しようとしています。

あなたがそれを見たいのであれば、私はすでに私の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 に答える