Python:使用OAuth2编辑在Raspberry Pi上运行的谷歌电子表格

时间:2016-02-13 18:08:20

标签: python python-2.7 google-sheets raspberry-pi google-spreadsheet-api

我们正在尝试将通过Raspberry Pi上的串行连接接收的传感器数据上传到Google电子表格。我们发现了各种不同的OAuth2身份验证教程,可以从python脚本访问Google电子表格。 但是,我们无法通过一条错误消息告诉我们,我们在验证过程中遇到了一个关键错误。

这是我们目前的代码:(这是一个空的测试谷歌帐户)

#! /usr/bin/python
import sys
import serial
import json
import gspread
import time
import datetime
import oauth2client.client
JSON_FILENAME = 'prox-a4c5a90245f4.json'
GSHEET_NAME = 'prox_logging_demo'

def proximity():
    ser = serial.Serial("/dev/ttyAMA0", 9600, timeout=1)
        response = ser.read()
        return response     

prox = proximity()

json_key = json.load(open(JSON_FILENAME))
creds = oauth2client.client.SignedJwtAssertionCredentials(json_key['prox-                    425@prox-1220.iam.gserviceaccount.com'],     json_key['MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCGmg31XacimqCk\nSUouX09//XXiLP8g4jTBvJUrNUUPoZMJX/3DlS5UKFxMEKorsvHR7vTQWb1M9PIi\nzGwwY6zkngBmw13tnuZXWqAUivHgzDc0MeX77+MrpsOBQWVF1JA7VxMwqwbY+EM6\nCwsXpBeF3O//pIZnAdzeP4lHub+9PUTrbHI1mXqvg9qyGibyCvA7aswdfA4XhuD0\nvCCjLDYT4VrxiHjSdWtweoESKBl343JDygFzhy1Vd+ukq9Vyw47bCBx/xC/neJa3\nCVWG0s608FjBxpCOGvCSEYnWoP3XPp3IOOWYjiJN+ms+pBQx9AcWzKjFHfUAckfA\nKZ2/5mJPAgMBAAECggEAf+CmeEEkg5yyF8LFZi8IwYlU0K99J4mhI5p9gZJXbVVM\n8zAuT/1QCYZawa1nbqLQWWmTTsqjLRTdYjAibQqVfJGbwgXEG/fF0KH5yeqDh9Wc\ncMsjO2qZwaPqw8cUZvZenPHJ+ofRtpqOi/tn62rrKjNQE+kEA6Liao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NoL5PJByh CQ'], ['https://spreadsheets.google.com/feeds'])
client_inst = gspread.authorize(creds) 
gsheet = client_inst.open(GSHEET_NAME).sheet1

curr_time = datetime.datetime.now()
print "Writing new row to %s: %s - %3.lf" % (GSHEET_NAME,curr_time, prox)

gsheet.append_row((curr_time, prox))

这是错误消息:

Traceback (most recent call last):
  File "Spreadsheet1.py", line 22, in <module>
    creds =     oauth2client.client.SignedJwtAssertionCredentials(json_key['prox-425@prox-1220.iam.gserviceaccount.com'], json_key['MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCGmg31XacimqCk\nSUouX09//XXiLP8g4jTBvJUrNUUPoZMJX/3DlS5UKFxMEKorsvHR7vTQWb1M9PIi\nzGwwY6zkngBmw13tnuZXWqAUivHgzDc0MeX77+MrpsOBQWVF1JA7VxMwqwbY+EM6\nCwsXpBeF3O//pIZnAdzeP4lHub+9PUTrbHI1mXqvg9qyGibyCvA7aswdfA4XhuD0\nvCCjLDYT4VrxiHjSdWtweoESKBl343JDygFzhy1Vd+ukq9Vyw47bCBx/xC/neJa3\nCVWG0s608FjBxpCOGvCSEYnWoP3XPp3IOOWYjiJN+ms+pBQx9AcWzKjFHfUAckfA\nKZ2/5mJPAgMBAAECggEAf+CmeEEkg5yyF8LFZi8IwYlU0K99J4mhI5p9gZJXbVVM\n8zAuT/1QCYZawa1nbqLQWWmTTsqjLRTdYjAibQqVfJGbwgXEG/fF0KH5yeqDh9Wc\ncMsjO2qZwaPqw8cUZvZenPHJ+ofRtpqOi/tn62rrKjNQE+kEA6Liaoekbe9L+YVy\nw9+dr/j827rok0DH8JlZ9UzRaBzOAYGw6B6J04H/+9Pn2L5xPCkzj0susItRF+XO\nJkGmtw+yRgyx+3RwQHuLuhDk5OC52Tgpv0uiEO/COTDaTepGitb5qOpXVfQ5kzeb\nqbJz0m+dowr16nncgsWK+tz0UvQzLcjQtOui2GApkQKBgQDWB4+pF8OXN8AZp494\nP8xm+5hp7hDgdJJSLTl34XDs0wuExp2yf/SG0/FEV/+G9+yZaKyXJ5SGswnywYpL\nP9BLIP5crO+HwkW9k2K9ZOQIp7W3zOtUXSf/R59xgNSAEdOBoWkmPifvXzFBHhVA\n5Z83FEqdv3O9DrqbDJSeKaBudwKBgQCg/ylufIbbMkvSYhnIC/l1iBNih+hPWKCQ\nf67kVWI2+D/t58HV7nd4a8+IfQ1PTq7huGKWuDAbAYKf5kIZydelaVdoweejFYu2\nT+w9MKYYs/2gi9gOEzt+XkBPleNSHuXwrKSxX8mnaEXLl3hcFYcWAxeTpYV8KvvW\nsshwgOro6QKBgF6elx9NFzYJ+o4c6qthoQ7CGb5y9dkVD/7uaWKbMt4v6v63A85/\nDO5yUk5BjQDxKBMmGG72m431JufAMlBUFNSx8GGuPnLJKyXFO7i5GqrM4eQw3rS8\nPkt4MX/vUYKQ3u2HG0JB5kvHzLGaCBFZskqaWOZmVld1ZH/ZTvahNoC/AoGADznS\ne0Z/on9ROGZatdacEwd5fdoImGhzVvccXS/OCBDWSvOeG1NgLR1VFT1uYs+4LlS3\nzjVBJ2yvI9ws6C90m4oo4BlmhL/m5KkOWIMfUjrDg8l1927OM5dKUbyFMSEG8goB\n0VyTv6dW4nhCw8KtceF8+1OuGh4ejyF9X1Zx4PECgYAKODEWhmLTQwPW3YDzZ71Q\nBc8pge7bdNX8bszRTRwiMJ3SCgalQztHuGwBxMPaIGLH9lhdgVDNm4Tr9wbcAF24\nrMnjP2DGsZx1cSs0Jftd2MnOyo4934XL5m6xsmF+wye2C4zFn8kcizzb4g1E0aWd\nAMyhyNc83sjNoL5PJByh CQ'], ['https://spreadsheets.google.com/feeds'])
KeyError: 'prox-425@prox-1220.iam.gserviceaccount.com'

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您可以测试下一个代码:

from oauth2client.service_account import ServiceAccountCredentials

scopes = ['https://www.googleapis.com/auth/sqlservice.admin']

credentials = ServiceAccountCredentials.from_json_keyfile_name(
'/path/to/keyfile.json', scopes=scopes)

更多信息:https://developers.google.com/identity/protocols/OAuth2ServiceAccount#authorizingrequests