2016-08-10 4 views
0

gspread와 서비스 계정 키, 기타, json 파일을 사용하고 있습니다. 지속적으로 Google 스프레드 시트를 Python 2.7로 업데이트합니다. 나는 이것을 최신 Raspian Jessie를 운영하는 Raspberry Pi에서 벗어나게했습니다. 내 oauth 및 gspread는 모두 내 플랫폼에서 사용할 수있는 최신 버전이어야합니다. 내 스크립트가 다음 오류 메시지와 함께 작동을 중지 한 시간 (최대 토큰 수명)에 대한 실행 : "잘못된 토큰 : Statless 토큰 만료 오류가"내 코드는 다음과 같다gspread/OAuth2Client 서비스 계정 키

import gspread 
from oauth2client.service_account import ServiceAccountCredentials 
import httplib2 
from httplib2 import Http 

scope = ['https://spreadsheets.google.com/feeds'] 
credentials = ServiceAccountCredentials.from_json_keyfile_name(filename.json,scope) 
gc = gspread.authorize(credentials) 
wks = gc.open('spreadsheet name') 
p1 = wks.worksheet('Printer One') 

def functon() 
... 
p1.append_row(printing) 

어떤 도움을 크게 감상 할 수있다, 고맙습니다.

답변

1

인증은 0.5/1 시간마다 만료됩니다 (연결하는 데 사용할 수있는 두 가지 방법 중 어떤 것이 맞는지에 달려 있다고 생각합니다).

24 시간마다 연결된 Google 시트가 2 초마다 업데이트됩니다. 거의 항상 잘못된 읽기/쓰기의 이유는 인증 오류뿐 아니라 Google API에서도 몇 초 후에 정상적으로 해결되는 다양한 오류가 발생할 수 있습니다. 다음은 셀을 업데이트하는 기능 중 하나입니다. 내 세부 정보는 auth_for_worksheet입니다. 모든 작업 (단일 셀 업데이트, 범위 업데이트, 값 열 읽기)에는 항상 권한있는 워크 시트를 반환하는 함수와 비슷한 구조가 있습니다. 그것은 아마도 가장 우아한 해결책은 아니지만 시트는 가동 중단없이 3 개월 동안 잘 연결되어 있습니다.

def auth_for_worksheet(): 
    scope = ['https://spreadsheets.google.com/feeds'] 
    credentials = ServiceAccountCredentials.from_json_keyfile_name(filename.json,scope) 
    gc = gspread.authorize(credentials) 
    wks = gc.open('spreadsheet name') 
    p1 = wks.worksheet('Printer One') 
    return p1 

def update_single_cell(worksheet, counter, message): 
    """ No data to return, update a single cell in column B to reflect this """ 

    single_cell_updated = False 
    while not single_cell_updated: 
     try: 
      cell_location = "B" + str(counter) 
      worksheet.update_acell(cell_location, message) 
      single_cell_updated = True 
     except gspread.exceptions.HTTPError: 
      logger.critical("Could not update single cell") 
      time.sleep(10) 
      worksheet = auth_for_worksheet() 
    logger.info("Updated single cell") 
    return worksheet 

if __name__ == '__main__': 

    # your code here, but now to update a single cell 
    wksheet = update_single_cell(wksheet, x, "NOT FOUND")