对不同的电子表格使用相同的凭据

时间:2018-10-06 11:21:44

标签: python google-sheets google-sheets-api

仅通过更改SPREADSHEET_ID来尝试访问两个不同的电子表格。 一个电子表格可以正常工作,而其他电子表格可以在我的PyCharm控制台中返回错误:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.1.3\helpers\pycharm\_jb_unittest_runner.py", line 35, in <module>
    main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING)
  File "C:\Users\gedas\AppData\Local\Programs\Python\Python36-32\lib\unittest\main.py", line 93, in __init__
    self.parseArgs(argv)
  File "C:\Users\gedas\AppData\Local\Programs\Python\Python36-32\lib\unittest\main.py", line 140, in parseArgs
    self.createTests()
  File "C:\Users\gedas\AppData\Local\Programs\Python\Python36-32\lib\unittest\main.py", line 147, in createTests
    self.module)
  File "C:\Users\gedas\AppData\Local\Programs\Python\Python36-32\lib\unittest\loader.py", line 219, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "C:\Users\gedas\AppData\Local\Programs\Python\Python36-32\lib\unittest\loader.py", line 219, in <listcomp>
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "C:\Users\gedas\AppData\Local\Programs\Python\Python36-32\lib\unittest\loader.py", line 153, in loadTestsFromName
    module = __import__(module_name)
  File "D:\pyth_nonsens\workspace_python\PyhonTutorial\google\test_spread.py", line 164, in <module>
    ss.get()
  File "D:\pyth_nonsens\workspace_python\PyhonTutorial\google\test_spread.py", line 145, in get
    result = service.spreadsheets().values().get(  spreadsheetId=SPREADSHEET_ID, range=RANGE_NAME).execute()
  File "C:\Users\gedas\AppData\Local\Programs\Python\Python36-32\lib\site-packages\oauth2client\_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "C:\Users\gedas\AppData\Local\Programs\Python\Python36-32\lib\site-packages\googleapiclient\http.py", line 840, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 404 when requesting https://sheets.googleapis.com/v4/spreadsheets/IAD2okAWZD7anbt5L4ybgD2dxHBGmsY6IkNIWHBQkBM/values/A1%3AE?alt=json returned "Requested entity was not found.">

我已经从登录资源管理器中打开链接并出现错误:

{
  "error": {
    "code": 403,
    "message": "The request is missing a valid API key.",
    "status": "PERMISSION_DENIED"
  }
}
  • 为什么python告诉页面不可用?
  • 问什么有效的API密钥系统?
  • 这些电子表格之间有什么区别?可以打开的一个是google示例,第二个是我创建的。

  • 为什么我可以使用相同的credentials.json文件和相同的Google帐户从已经完成的C#程序中打开两个电子表格?

我的Python代码

class SprSht:
    def get(self):
        print("starting get")
        store = file.Storage('token.json')
        try:
            creds = store.get()
        except Exception as e:
            print(traceback.format_exc())

        if not creds or creds.invalid:
            flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
            creds = tools.run_flow(flow, store)
        service = build('sheets', 'v4', http=creds.authorize(Http()))
        # Call the Sheets API
        SPREADSHEET_ID = 'IAD2okAWZD7anbt5L4ybgD2dxHBGmsY6IkNIWHBQkBM'  # my
        #SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'

        RANGE_NAME = 'A1:E'
        result = service.spreadsheets().values().get(  spreadsheetId=SPREADSHEET_ID, range=RANGE_NAME).execute()
        values = result.get('values', [])
        if not values:
            print('No data found.')
        else:
            print('Name, Major:')
            for row in values:
                # Print columns A and E, which correspond to indices 0 and 4.
                print('%s, %s' % (row[0], row[4]))

ss = SprSht()
ss.get()

0 个答案:

没有答案