【问题标题】:Trying to Access Google Sheets and Getting Error About 'Unexpected Credentials Type'尝试访问 Google 表格并收到有关“意外凭据类型”的错误
【发布时间】:2020-03-11 15:27:25
【问题描述】:

我正在尝试运行以下代码来访问 Google 表格。

import os
os.getcwd()
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd

scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']

credentials = ServiceAccountCredentials.from_json_keyfile_name('client_secret_406147946600-na19mdik9a4571623b4kdp30h5354el0.apps.googleusercontent.com.json', scope) # Your json file here
credentials

我从“OAuth 2.0 客户端 ID”中获得了 .json 文件。我所做的只是单击“下载 JSON”,然后将文件放在名为“C:\Users\ryans”的默认文件夹中。当我运行代码时,我得到了这个错误。

Traceback (most recent call last):

  File "<ipython-input-17-26266215508f>", line 9, in <module>
    credentials = ServiceAccountCredentials.from_json_keyfile_name('client_secret_406147946600-na19mdik9a4571623b4kdp30h5354el0.apps.googleusercontent.com.json', scope) # Your json file here

  File "C:\Users\ryans\Anaconda3\lib\site-packages\oauth2client\service_account.py", line 223, in from_json_keyfile_name
    revoke_uri=revoke_uri)

  File "C:\Users\ryans\Anaconda3\lib\site-packages\oauth2client\service_account.py", line 172, in _from_parsed_json_keyfile
    'Expected', client.SERVICE_ACCOUNT)

ValueError: ('Unexpected credentials type', None, 'Expected', 'service_account')

我所做的只是给它提供与它给我完全相同的文件。就是这样。如果我将名称更改为:'service_account.json',则会收到以下错误。

ValueError: ('Unexpected credentials type', None, 'Expected', 'service_account')

我所做的正是它告诉我要做的。我不明白这里有什么问题。这似乎是一个 2 秒的工作(复制/粘贴),但这里根本没有任何工作。我在网上看到过其他示例,其他人表示一切正常,但无论我做什么,都对我不起作用。是否有某种特殊的安全设置或类似的东西?我可以禁用一些安全性(如果它存在的话)吗?

【问题讨论】:

    标签: python python-3.x google-sheets google-sheets-api


    【解决方案1】:
    • 您希望通过服务帐号使用 gspread 来使用 Sheets API。

    如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。

    问题及解决方案:

    在你的问题中,我可以确认如下。

    我从“OAuth 2.0 客户端 ID”中获得了 .json 文件。我所做的只是单击“下载 JSON”,然后将文件放在名为“C:\Users\ryans”的默认文件夹中。当我运行代码时,我得到了这个错误。

    在这种情况下,我认为下载的文件不是服务帐户的凭据。这用于 OAuth2 过程。我认为客户端 ID 和客户端密码包含在文件中。当我为您的脚本测试此文件时,我可以确认发生了同样的问题。

    所以请创建服务帐户并获取 JSON 文件,如下所示。

    注意:

    是否有某种特殊的安全设置或类似的东西?我可以禁用某些安全性(如果它存在的话)吗?

    您也可以做到这一点。也可以看the official document

    参考资料:

    如果我误解了您的情况并且这不是您想要的方向,我深表歉意。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-26
      • 2021-08-25
      • 2022-12-16
      • 2015-12-11
      • 2010-10-24
      • 1970-01-01
      • 2018-09-08
      • 1970-01-01
      相关资源
      最近更新 更多