代码之家  ›  专栏  ›  技术社区  ›  vico

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

  •  0
  • vico  · 技术社区  · 6 年前

    仅通过更改来访问两个不同的电子表格 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告诉page不可用?
    • 什么是有效的api密钥系统?
    • 这些电子表格有什么区别?打开的一个是google示例,第二个是我创建的。

    • 为什么我可以打开我用同一个程序做的两个电子表格 credentials.json 文件和相同的谷歌帐户?

    我的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 回复  |  直到 6 年前